I have a problem with array of arrays object. I tried the default Java array like this:
String[][] array = new String[5][5];
And it worked. But now I'm facing for another problem. I don't know the sizes of the array so I need a dynamically allocated array of arrays. I tried this:
ArrayList<String> array = new ArrayList<>();
ArrayList<ArrayList<String>> arrayOfArrays = new ArrayList<>();
array.add("1");
array.add("2");
array.add("3");
arrayOfArrays.add(array);
System.out.println(arrayOfArrays);
array.clear();
array.add("4");
array.add("5");
array.add("6");
arrayOfArrays.add(array);
System.out.println(arrayOfArrays);
And it prints:
[[1, 2, 3]]
[[4, 5, 6], [4, 5, 6]]
And I don't need to rewrite it. It should looks like this:
[[1, 2, 3]]
[[1, 2, 3], [4, 5, 6]]
I'm facing for this problem very long time and I tried a lot of workarounds but I need some clever solution. Also I will appreciate any help.
And I have a second question. How to add it in cycle? Because it has the same output as in the first case. For example:
for (int i = 0; i < array.size() - 1; i++) {
arrayOfArrays.add(swap(array, i, i+1));
}