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?

Recommended Answers

All 9 Replies

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.

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

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

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.

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.

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.

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!

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.

commented: Useless bump to post useless information. -3

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.