943,851 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1381
  • Java RSS
Jul 5th, 2009
0

variable substitution

Expand Post »
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)
  1. private ArrayList row0 = new ArrayList<String>();
  2. private ArrayList row1 = new ArrayList<String>();
  3. // 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)
  1. for (int i = 0; i < 7; i++) {
  2. row0.add(".");
  3. row1.add(".");
  4. // etc.
  5. }

instead I would like to be able do something like this....

java Syntax (Toggle Plain Text)
  1. for (int i = 0; i < 8; i++) {
  2. for (int j = 0; j < 7; j++) {
  3. rowi.add(".");
  4. }
  5. }

how do I substitute that "i" in there though?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
gotm is offline Offline
33 posts
since May 2008
Jul 5th, 2009
0

Re: variable substitution

Click to Expand / Collapse  Quote originally posted by gotm ...
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)
  1. private ArrayList row0 = new ArrayList<String>();
  2. private ArrayList row1 = new ArrayList<String>();
  3. // 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)
  1. for (int i = 0; i < 7; i++) {
  2. row0.add(".");
  3. row1.add(".");
  4. // etc.
  5. }

instead I would like to be able do something like this....

java Syntax (Toggle Plain Text)
  1. for (int i = 0; i < 8; i++) {
  2. for (int j = 0; j < 7; j++) {
  3. rowi.add(".");
  4. }
  5. }

how do I substitute that "i" in there though?

Make row an array:

Java Syntax (Toggle Plain Text)
  1. private ArrayList row[] = new ArrayList<String>[8];
  2.  
  3. for (int i = 0; i < 8; i++)
  4. {
  5. row[i] = new ArrayList<String> ();
  6. for (int j = 0; j < 7; j++)
  7. row[i].add (".");
  8. }

Or make row an ArrayList of ArrayLists.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Jul 6th, 2009
0

Re: variable substitution

...or...
use nested arraylists
ArrayList<ArrayList<String>> rows
and access the inner ArrayLists, and their Strings in the usual way.
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is online now Online
5,785 posts
since Apr 2008
Jul 6th, 2009
0

Re: variable substitution

...or...
use nested arraylists
ArrayList<ArrayList<String>> rows
and access the inner ArrayLists, and their Strings in the usual way.
James, out of curiosity, isn't that the same thing as what Vernon suggested?
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jul 7th, 2009
0

Re: variable substitution

James, out of curiosity, isn't that the same thing as what Vernon suggested?
Very similar, except that I used an ArrayList where Vernon used an array. I have a strong prejudice in favour of ArrayLists or Vectors as opposed to arrays - saves all that housekeeping for nextEmptyElement and maxSizeExceeded, and Vectors are thread safe. I always suspect that any use of an array in Java should really be a Vector if it's variable sized, or an enum if it's fixed.
But that's just my opinion.
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is online now Online
5,785 posts
since Apr 2008
Jul 7th, 2009
0

Re: variable substitution

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.
Reputation Points: 874
Solved Threads: 352
Posting Maven
BestJewSinceJC is offline Offline
2,758 posts
since Sep 2008
Jul 7th, 2009
0

Re: variable substitution

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.

No difference. James and I were thinking the same thing here.
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Jul 8th, 2009
0

Re: variable substitution

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.
He developed the code for the array version in some detail, so I just filled in a little more detail on the version that he mentioned with his "or...". I've found that many people don't realise that you can specify nested generics, let alone how to do it.
I think we're all in complete agreement!
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is online now Online
5,785 posts
since Apr 2008
Oct 10th, 2011
-2
Re: variable substitution
this absolutely doesn't solve the issue if you for example have set of jTextArea1 jTextArea2 and so on.... you can't just conjure it into array from a thin air....

some variables can't be array'd not like in bash..... awww I miss bash so much.
Reputation Points: 7
Solved Threads: 0
Newbie Poster
tommmmmm is offline Offline
1 posts
since Oct 2011
Oct 10th, 2011
0
Re: variable substitution
1. This is a long dead thread
2. You can put anything in a Java array. If you have a Set of <anything> you can get it as an array with its the toArray() method. Please don't confuse beginners here with incorrect statements.
Featured Poster
Reputation Points: 1924
Solved Threads: 951
Posting Expert
JamesCherrill is online now Online
5,785 posts
since Apr 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Java3d for web browsers
Next Thread in Java Forum Timeline: loops





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC