954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

variable substitution

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:

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:

for (int i = 0; i < 7; i++) {
    row0.add(".");
    row1.add(".");
    // etc.
}


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

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?

gotm
Light Poster
33 posts since May 2008
Reputation Points: 10
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:

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:

for (int i = 0; i < 7; i++) {
    row0.add(".");
    row1.add(".");
    // etc.
}

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

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:

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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 

...or...
use nested arraylists
ArrayList> rows
and access the inner ArrayLists, and their Strings in the usual way.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 
...or... use nested arraylists ArrayList> 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?

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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.

BestJewSinceJC
Posting Maven
2,772 posts since Sep 2008
Reputation Points: 874
Solved Threads: 354
 
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.

VernonDozier
Posting Expert
5,527 posts since Jan 2008
Reputation Points: 2,633
Solved Threads: 711
 
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!

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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.

tommmmmm
Newbie Poster
1 post since Oct 2011
Reputation Points: 7
Solved Threads: 0
 

1. This is a long dead thread
2. You can put anything in a Java array. If you have a Set of you can get it as an array with its the toArray() method. Please don't confuse beginners here with incorrect statements.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You