| | |
variable substitution
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: May 2008
Posts: 33
Reputation:
Solved Threads: 0
hey guys. OK so, I've been wondering this for a while now. my code is always so unnecessarily cluttered because I don't know how to do this, lol.
say I have a set of private class variables:
Now i have a loop where i want to assign (for example), 8 periods or something to each array list as separate elements. I don't want to do this same code and repeat it 8 times for each list, like this:
instead I would like to be able do something like this....
how do I substitute that "i" in there though?
say I have a set of private class variables:
java Syntax (Toggle Plain Text)
private ArrayList row0 = new ArrayList<String>(); private ArrayList row1 = new ArrayList<String>(); // etc. all the way to 7 in this case
Now i have a loop where i want to assign (for example), 8 periods or something to each array list as separate elements. I don't want to do this same code and repeat it 8 times for each list, like this:
java Syntax (Toggle Plain Text)
for (int i = 0; i < 7; i++) { row0.add("."); row1.add("."); // etc. }
instead I would like to be able do something like this....
java Syntax (Toggle Plain Text)
for (int i = 0; i < 8; i++) { for (int j = 0; j < 7; j++) { rowi.add("."); } }
how do I substitute that "i" in there though?
•
•
Join Date: Jan 2008
Posts: 3,828
Reputation:
Solved Threads: 501
•
•
•
•
hey guys. OK so, I've been wondering this for a while now. my code is always so unnecessarily cluttered because I don't know how to do this, lol.
say I have a set of private class variables:
java Syntax (Toggle Plain Text)
private ArrayList row0 = new ArrayList<String>(); private ArrayList row1 = new ArrayList<String>(); // etc. all the way to 7 in this case
Now i have a loop where i want to assign (for example), 8 periods or something to each array list as separate elements. I don't want to do this same code and repeat it 8 times for each list, like this:
java Syntax (Toggle Plain Text)
for (int i = 0; i < 7; i++) { row0.add("."); row1.add("."); // etc. }
instead I would like to be able do something like this....
java Syntax (Toggle Plain Text)
for (int i = 0; i < 8; i++) { for (int j = 0; j < 7; j++) { rowi.add("."); } }
how do I substitute that "i" in there though?
Make row an array:
Java Syntax (Toggle Plain Text)
private ArrayList row[] = new ArrayList<String>[8]; for (int i = 0; i < 8; i++) { row[i] = new ArrayList<String> (); for (int j = 0; j < 7; j++) row[i].add ("."); }
Or make row an ArrayList of ArrayLists.
•
•
Join Date: Apr 2008
Posts: 977
Reputation:
Solved Threads: 145
•
•
•
•
James, out of curiosity, isn't that the same thing as what Vernon suggested?
But that's just my opinion.
•
•
Join Date: Apr 2008
Posts: 977
Reputation:
Solved Threads: 145
•
•
•
•
Oh, I was referring to where he said "Or make row an ArrayList of ArrayLists." I thought maybe there was some difference in syntax between how you did it and he did it or something. Nevermind.
I think we're all in complete agreement!
![]() |
Similar Threads
- Using varibles within awk/sed commands (Shell Scripting)
- URL Encoding and/or string substitution (JSP)
- Crazy Leak...plz help (C++)
- php results from form - stuck (PHP)
- Why won't I be prompted for input (Oracle)
- Retreiving a Variable from a URL (PHP)
- Can draw one Frame in a variable? (Java)
- Some help understanding #define with C (C)
- Pipe inside variable isn't working in bash (Shell Scripting)
Other Threads in the Java Forum
- Previous Thread: SwingUtilities.invokeLater() and JLabel.setText()
- Next Thread: Array output
| Thread Tools | Search this Thread |
android api applet application array arrays automation awt bidirectional binary birt bluetooth businessintelligence busy_handler(null) chat class classes client code columns component constructor database designadrawingapplicationusingjavajslider draw eclipse editor error errors event eventlistener exception expand fractal game givemetehcodez graphics gui guidancer html ide image inetaddress input integer intellij j2me java javafx javamicroeditionuseofmotionsensor javaprojects jme jni jpanel jtree julia linux list loop map method methods mobile mobiledevelopmentcreatejar myaggfun netbeans newbie oracle parsing plazmic print problem program programming project recursion scanner screen server set sharepoint size smart sms smsspam sort sortedmaps sql string subclass support swing textfield threads time tree unlimited utility webservices windows






