I have this defined: List<Integer[][]> listOfInteger= new ArrayList<>();
How do I insert data into it?
I have tried
listOfInteger.add(new Integer[][](1,2));
and
List<Integer[][]> listOfInteger= Arrays.asList(new Integer({1, 2)});
But none works
Have you tried the "double brace initialization"?
List<Integer[][]> listOfInteger= new ArrayList() {{
add(new Integer[][] { { 3, 2 }, { 4, 2 } });
add(new Integer[][] { { 3, 2 }, { 4, 2 } });
add(new Integer[][] { { 3, 2 }, { 4, 2 } });
}};
You can do this at the class level outside of methods.
The short way is this:
listOfInteger.add(new Integer[][] {{1, 2}, {1, 3}, {5, 7}});
But you could also just create the array and then fill it one by one.
.add method? I am trying to initialize listOfInteger and give it values at the beginning of the class and outside of method blocks.static { } in there you can do the adds. Also works for static final, create temp list, add everything, assign to static final var.You got confused by the dimensions you have defined an one dimensional ArrayLists which contains two-dimensional arrays as elements.
List<Integer[][]> listOfInteger = new ArrayList<Integer[][]>();
// long version
Integer[][] array2d = new Integer[2][2];
array2d[0][0] = 3;
array2d[0][1] = 2;
array2d[1][0] = 4;
array2d[1][1] = 2;
listOfInteger.add(array2d);
// short version
listOfInteger.add(new Integer[][] { { 3, 2 }, { 4, 2 } });
After this operations your arrayList will contain two arrays, which contain integers with the same value
List<ArrayList<Integer>>orInteger [][]int, i have to make itIntegerlistOfInteger.add(new Integer[][]{{1,2},{3,4}});