user4934415
user4934415

Reputation:

How can I clear one variable (List Of Array Lists), without unintentionally clearing another?

To be clear, I have one 3D ArrayList which I intend to use: to hold several 2D ArrayLists.

3D ArrayList (When I say 3D ArrayList, this is what I'm referring to):

ArrayList<ArrayList<ArrayList<String>>> main_Functions = new ArrayList<>();

2D ArrayList (When I say 2D ArrayList, this is what I'm referring to):

ArrayList<ArrayList<String>> localValues = new ArrayList<>();

But after adding each 2D ArrayList, I intend to clear the variable holding the original 2D ArrayList. So old information doesn't interfere with newly added information etc. But after I clear the variable holding the original 2D ArrayList (what 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 information added to the 3D ArrayList, remains upon the 2D ArrayList's clearing?

Upvotes: 4

Views: 263

Answers (2)

Srini
Srini

Reputation: 1636

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 creating a copy of your localValues list, the data is duplicated and clearing it won't affect this new data.

Although I don't understand why you would want to clear the localValues list, clearing localValues won't change the main_Functions list.

Upvotes: 0

Your situation is basically this: (I've removed one level of ArrayLists 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 simpler:

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 ArrayLists. In Java, if we say a variable holds an ArrayList, what we actually mean is it holds a reference to an ArrayList.

In other words, what a actually contains 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 could do something like

ArrayList<ArrayList<String>> localValues_copy = new ArrayList<>(localValues);
main_Functions.add(localValues_copy);

or shortened:

main_Functions.add(new ArrayList<>(localValues));

this will add a reference to a brand new ArrayList to your "3D ArrayList", instead of adding a reference to the same one localValues refers to.

Upvotes: 4

Related Questions