| | |
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,842
Reputation:
Solved Threads: 503
•
•
•
•
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: 1,020
Reputation:
Solved Threads: 151
•
•
•
•
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: 1,020
Reputation:
Solved Threads: 151
•
•
•
•
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
Views: 519 | Replies: 7
| Thread Tools | Search this Thread |
Tag cloud for Java
-xlint android animated api appinventor apple applet application arguments array arrays automation bi binary blackberry bluetooth chat class classes client code compile compiler component database draw eclipse error event exception file fractal freeze functiontesting game gameprogramming givemetehcodez graphics gui helpwithhomework html ide image input integer j2me java javaprojects jetbrains jmf jni jpanel jtable julia learningresources linux list login loop main map method methods mobile myregfun netbeans newbie notdisplaying number object oracle page print problem program programming project qt recursion scanner screen server set size sms socket sort spamblocker sql string swing system test threads time transfer tree variablebinding windows xor






