variable substitution

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: May 2008
Posts: 33
Reputation: gotm is an unknown quantity at this point 
Solved Threads: 0
gotm gotm is offline Offline
Light Poster

variable substitution

 
0
  #1
Jul 5th, 2009
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:

  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:

  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....

  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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: variable substitution

 
0
  #2
Jul 5th, 2009
Originally Posted by gotm View 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:

  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:

  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....

  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:

  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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 977
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: variable substitution

 
0
  #3
Jul 6th, 2009
...or...
use nested arraylists
ArrayList<ArrayList<String>> rows
and access the inner ArrayLists, and their Strings in the usual way.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: variable substitution

 
0
  #4
Jul 6th, 2009
Originally Posted by JamesCherrill View Post
...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?
Out.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 977
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: variable substitution

 
0
  #5
Jul 7th, 2009
Originally Posted by BestJewSinceJC View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 1,598
Reputation: BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all BestJewSinceJC is a name known to all 
Solved Threads: 202
BestJewSinceJC BestJewSinceJC is offline Offline
Posting Virtuoso

Re: variable substitution

 
0
  #6
Jul 7th, 2009
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.
Out.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,828
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: variable substitution

 
0
  #7
Jul 7th, 2009
Originally Posted by BestJewSinceJC View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 977
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: variable substitution

 
0
  #8
Jul 8th, 2009
Originally Posted by BestJewSinceJC View Post
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!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC