Results 1 to 8 of 8
- 05-01-2009, 08:24 PM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
[SOLVED] Append an incremental number to variable name
Is it possible to append the value of a variable to the name of another variable?
I want to create several arrays from one input file, but I want to do it all in one nested loop structure that increments a value and appends it to the word "array" and uses that to create a new array. How would I do the following:
Java Code:int arrayNumber; int[][] array (want to append arrayNumber here) = new int[x][x]
Last edited by porchrat; 05-01-2009 at 08:26 PM.
- 05-01-2009, 08:42 PM #2
Member
- Join Date
- Nov 2008
- Posts
- 44
- Rep Power
- 0
Not sure, but I think that you can't dinamically change the name of a variable. So you got to rethink your architecture.
But why do you want to do that?
- 05-01-2009, 08:58 PM #3
no sadly this is not possible(I've looked into ways for doing it). It just seems terribly inefficient to write
JButton button1 = new JButton();
JButton button2 = new JButton();
....
JButton button99 = new JButton();
I wish Sun would look into implementing this. Something like a
String String.setVariableName("button"+i) = new String();
would be nice.Liberty has never come from the government.
Liberty has always come from the subjects of government.
The history of liberty is the history of resistance.
The history of liberty is a history of the limitation of governmental power, not the increase of it.
- 05-01-2009, 09:32 PM #4
So use a multidimensional array. Arrays aren't limited to 2 dimensions, you know.
db
-
Variable names are next to meaningless when your code is run. If the JVM doesn't care about them, why should you? If you want to associated a variable with a String, I'd suggest that you use a Map. If you want to associate a number, then use arrays as Darryl suggests above.
- 05-02-2009, 07:23 PM #6
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
Unfortunately I'm not allowed to, this has to do with an assignment for university and I have to use 2D arrays only.
I've since realised that I don't really need to store the array at all, therefore there is no need to create a new array each time.
I've just created a class that overwrites the array, runs the methods that need to run against the array, overwrites it again etc. etc. until it is finished processing all the arrays it needs to handle.
That way I don't need to create a new array each time and hence I don't need to change the variable name.
If I were allowed to I would've created a three-dimensional array int[][][] and incremented the final value by one each time I wanted to create a 'new' array, unfortunately I wasn't allowed to.Last edited by porchrat; 05-02-2009 at 07:26 PM.
-
I'm glad you've got it figured out. This or any forum will probably be able to better help you in the future if you specify all of your requirements in the first post. Best of luck.
- 05-06-2009, 05:32 PM #8
Senior Member
- Join Date
- Mar 2009
- Posts
- 105
- Rep Power
- 0
Actually I find this forum exceptionally helpful. I'm always grateful for any help I get, whether it is applicable to my particular problem or not, knowledge is knowledge and, being a beginner, I am eager to absorb as many different approaches to programming problems as I can.
More often than not I find people here can answer my questions. Unfortunately I think the reason my post wasn't fully explained is that my knowledge is not sufficient yet to explain my problem fully.
I hadn't considered 3D arrays when I first posted because I didn't know 3D arrays existed, but I had reached that point (and asked my lecturer) by the time the reply arrived. Hopefully as my knowledge and experience grows so will the accuracy of my posts.:D
Thank you all for your suggestions, every response helps me to better understand java.Last edited by porchrat; 05-06-2009 at 05:34 PM.
Similar Threads
-
append variables to a text file
By ddatta8 in forum New To JavaReplies: 2Last Post: 01-02-2009, 11:17 AM -
how can i append new element and child from java
By Omarero in forum XMLReplies: 3Last Post: 11-21-2008, 08:43 AM -
Can we append more than 255 chars to <a href>??
By freddieMaize in forum Advanced JavaReplies: 22Last Post: 07-18-2008, 05:04 PM -
JLabel append?
By Jononomous in forum New To JavaReplies: 0Last Post: 04-07-2008, 08:41 PM -
Simple append question
By Rageagainst20 in forum New To JavaReplies: 0Last Post: 12-21-2007, 12:40 AM
Bookmarks