簡體   English   中英

Java - 如何迭代3維ArrayList?

[英]Java - How to iterate through 3-dimensional ArrayList?

我正在嘗試為我的項目實現ArrayList,但我在途中遇到了一些問題。 如果有人可以幫助我,那將是美好的。

我有這個三維數組的字符串,通過它迭代它以保持一些字符串值。

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";
    }
  } 
}

但由於其大小的靈活性,我決定使用ArrayList。

問題是,我不知道如何迭代3維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[0].size();y++){
    for(int z=0;z<arrSup[0][0].size();z++){
       array[x][y][z] = "Lorem ipsum";
    }
  } 
}

那么,誰能告訴我如何迭代三維ArrayList?

謝謝。

使用ArrayList::get(int index)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");
    }
  } 
}

你需要使用get沒有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");
    }
  } 
}

假設一個10x10x10陣列:

    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");
            }
        }
    }

與多維數組不同,列表不會為您分配,因此您必須自己添加子列表。

迭代可以通過兩種方式完成,具體取決於您是否需要坐標。

// 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);
        }
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM