嘗試這種方式
List<List<String>>ar1 = new ArrayList<>();
//lets say we want to have array [2, 4]
//we will initialize it with nulls
for (int i=0; i<2; i++){
ar1.add(new ArrayList<String>());
for(int j=0; j<4; j++)
ar1.get(i).add(null);
}
System.out.println("empty array="+ar1);
//lets edit contend of that collection
ar1.get(0).set(1, "position (0 , 1)");
ar1.get(1).set(3, "position (1 , 3)");
System.out.println("edited array="+ar1);
//to get element [0, 1] we can use: ar1.get(0).get(1)
System.out.println("element at [0,1]="+ar1.get(0).get(1));
輸出:
empty array=[[null, null, null, null], [null, null, null, null]]
edited array=[[null, position (0 , 1), null, null], [null, null, null, position (1 , 3)]]
element at [0,1]=position (0 , 1)
的元素,但我需要一個值插入ArrayList中。 ar1.get(k).get(j).add(something)不可能 –
ar1.get(k).set(j,something); – Doorknob
或ar1.get(k)。添點什麼);添加一個項目 – Doorknob