Since the number of columns is a constant, you can just have an List
of int[]
.
import java.util.*;
//...
List<int[]> rowList = new ArrayList<int[]>();
rowList.add(new int[] { 1, 2, 3 });
rowList.add(new int[] { 4, 5, 6 });
rowList.add(new int[] { 7, 8 });
for (int[] row : rowList) {
System.out.println("Row = " + Arrays.toString(row));
} // prints:
// Row = [1, 2, 3]
// Row = [4, 5, 6]
// Row = [7, 8]
System.out.println(rowList.get(1)[1]); // prints "5"
Since it's backed by a List
, the number of rows can grow and shrink dynamically. Each row is backed by an int[]
, which is static, but you said that the number of columns is fixed, so this is not a problem.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…