How can I clear one variable (List Of Array Lists) by accidentally clearing it?
To be clear, I have one 3D ArrayList that I intend to use: to store multiple 2D arrays.
3D ArrayList (When I say 3D ArrayList this is what I'm talking about):
ArrayList<ArrayList<ArrayList<String>>> main_Functions = new ArrayList<>();
2D ArrayList (When I say 2D ArrayList this is what I'm talking about):
ArrayList<ArrayList<String>> localValues = new ArrayList<>();
But after adding each 2D ArrayList, I intend to clear the variable containing the original 2D ArrayList. Thus, old information does not interfere with new added information, etc. But after I clear the variable containing the original 2D ArrayList (which was added to the 3D ArrayList) the data added to the 3D ArrayList is erased.
My code is below:
ArrayList<ArrayList<String>> localValues = new ArrayList<>();
ArrayList<ArrayList<ArrayList<String>>> main_Functions = new ArrayList<>();
if (arrayListNotEmpty(localValues)) {
main_Functions.add(localValues);
localValues.clear();
}
How can I fix this? so does the information added to the 3D ArrayList remain on the cleanup of the 2D ArrayList?
Your situation is basically like this: (I removed one level ArrayList
to make it clearer)
ArrayList<String> inner = new ArrayList<>();
ArrayList<ArrayList<String>> outer = new ArrayList<>();
inner.add("Hello World!");
outer.add(inner);
System.out.println(outer.get(0)); // prints [Hello World!]
inner.clear();
System.out.println(outer.get(0)); // prints [] i.e. an empty list
Or, even easier:
ArrayList<String> a = new ArrayList<>();
a.add("Hello World!");
ArrayList<String> b = a;
System.out.println(b); // prints [Hello World!]
a.clear();
System.out.println(b); // prints []
This is because, a
and b
do not contain ArrayList
s. In Java, if we say that a variable contains an ArrayList, we actually mean that it contains a reference to an ArrayList.
In other words, what actually contains a
is "ArrayList # 1234" or something like that. b
also contains "ArrayList # 1234". The line a.clear()
clears ArrayList # 1234, and the line System.out.println(b);
prints the contents of ArrayList # 1234 (which we just cleared).
Instead, you can create a new ArrayList whenever you want to copy it.
For example, instead of
main_Functions.add(localValues);
you can do something like
ArrayList<ArrayList<String>> localValues_copy = new ArrayList<>(localValues);
main_Functions.add(localValues_copy);
or abbreviated:
main_Functions.add(new ArrayList<>(localValues));
this will add a new ArrayList reference to your "3D ArrayList" instead of adding a reference to the same one localValues
.
source to share
List<List<String>> localValues = new ArrayList<>();
List<List<String>> localValuesCopy = new ArrayList<>(localValues);
localValues.clear();// won't affect main_Functions
List<List<List<String>>> main_Functions = new ArrayList<>();
main_Functions.add(localValuesCopy);
By making a copy of your list localValues
, the data will be duplicated and cleaning will not affect this new data.
While I don't understand why you want to clear the list localValues
, clearing the localValues
list will not change main_Functions
.
source to share