3

I'm trying to implement ArrayList for my project, but I had some problem on the way. If anyone can help me, that would be wonderful.

I have this 3-dimensional array of string, iterated through it to have it hold some String values.

for(int x=0;x<array.length;x++){
  for(int y=0;y<array[0].length;y++){
    for(int z=0;z<array[0][0].length;z++){
       array[x][y][z] = "Lorem ipsum";
    }
  } 
}

But due to its flexibility of size, I've decided to use ArrayList instead.

The problem is, I have no idea how to iterate through a 3-dimensional ArrayList. Thought something like this would work, but it didn't.

ArrayList<ArrayList<ArrayList<String>>> arrSup = new ArrayList<ArrayList<ArrayList<String>>>();

for(int x=0;x<arrSup.size();x++){
  for(int y=0;y<arrSup[0].size();y++){
    for(int z=0;z<arrSup[0][0].size();z++){
       array[x][y][z] = "Lorem ipsum";
    }
  } 
}

So, can anyone tell me how to iterate through a 3-dimensional ArrayList?

Thank you.

1
  • In Java 7 (and above) you can simplify the declaration into List<List<List<String>>> arrSup = new ArrayList<>(); thanks to the diamond operator. Commented Sep 29, 2015 at 17:53

4 Answers 4

1

Use ArrayList::get(int index) and ArrayList::set(int index, T value):

int INITIAL_X_SIZE = 100;
int INITIAL_Y_SIZE = 100;
int INITIAL_Z_SIZE = 100;
ArrayList<ArrayList<ArrayList<String>>> arrSup = new ArrayList<ArrayList<ArrayList<String>>>(INITIAL_X_SIZE);

// Initialize the ArrayLists:
for(int x = 0; x < INITIAL_X_SIZE; x++) {
  arrSup.set(x, new ArrayList<ArrayList<String>>(INITIAL_Y_SIZE));
  for(int y = 0;y < INITIAL_Y_SIZE; y++) {
    arrSup.get(x).set(x, new ArrayList<String>(INITIAL_Z_SIZE));
  }
}

// Iterate through it and do whatever you want to do:
for(int x = 0; x < arrSup.size(); x++) {
  for(int y = 0; y < arrSup.get(x).size(); y++) {
    for(int z = 0; z<arrSup.get(x).get(y).size(); z++) {
       array.get(x).get(y).set(z, "Lorem ipsum");
    }
  } 
}
Sign up to request clarification or add additional context in comments.

Comments

0

You need to use get there is no indexing for arrayList.

ArrayList<ArrayList<ArrayList<String>>> arrSup = new ArrayList<ArrayList<ArrayList<String>>>();

for(int x=0;x<arrSup.size();x++){
  for(int y=0;y<arrSup.get(0).size();y++){
    for(int z=0;z<arrSup.get(0).get(0).size();z++){
       array.get(x).get(y).set(z, "Lorem ipsum");
    }
  } 
}

1 Comment

Changed it, also noticed.
0

Suppose a 10x10x10 array:

    ArrayList<ArrayList<ArrayList<String>>> arrSup = new ArrayList<ArrayList<ArrayList<String>>>();

    for (int x = 0; x < 10; x++) {
        arrSup.add(new ArrayList<ArrayList<String>>());
        for (int y = 0; y < 10; y++) {
            arrSup.get(x).add(new ArrayList<String>());
            for (int z = 0; z < 10; z++) {
                arrSup.get(x).get(y).add("Lorem ipsum");
            }
        }
    }

Comments

0

Unlike multi-dimensional arrays, Lists are not allocated for you, so you have to add the sub-Lists yourself.

Iterating can be done in two ways, depending of whether you need to coordinates.

// Build matrix
final int SIZE_X = 10;
final int SIZE_Y = 10;
final int SIZE_Z = 10;
List<List<List<String>>> matrix = new ArrayList<>();
for (int x = 0; x < SIZE_X; x++) {
    List<List<String>> yList = new ArrayList<>();
    matrix.add(yList);
    for (int y = 0; y < SIZE_Y; y++) {
        List<String> zList = new ArrayList<>();
        yList.add(zList);
        for (int z = 0; z < SIZE_Z; z++)
            zList.add("Lorem ipsum");
    }
}

// Iterate matrix without coordinates
for (List<List<String>> yList : matrix)
    for (List<String> zList : yList)
        for (String value : zList) {
            System.out.println(value);
        }

// Iterate matrix with coordinates
for (int x = 0; x < matrix.size(); x++) {
    List<List<String>> yList = matrix.get(x);
    for (int y = 0; y < yList.size(); y++) {
        List<String> zList = yList.get(y);
        for (int z = 0; z < zList.size(); z++) {
            String value = zList.get(z);
            System.out.println(value);
        }
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.