0

I don't really know how to ask this without creating confusion, but I'll try.

In my Swing application I want to add an ArrayList to an ArrayList and then clear(); the second ArrayList without all the data being lost.

Here's an example:

ArrayList<ArrayList<String>> aList1 = new ArrayList<>();
ArrayList<String> aList2 = new ArrayList<>();
aList2.add("Object 1");
aList2.add("Object 2");
aList1.add(aList2);
aList2.clear();
System.out.println(aList1);

But then all the data is lost and aList1 is empty. I assume this is because I add the ArrayList as ArrayList and not as values. Is it possible to save the data in aList1 while deleting it from aList2, thus making space for future use.

Shiro
  • 2,610
  • 2
  • 20
  • 36
  • Hint: start reading javadocs for the library calls you are using. Most collections have an **addAll()** method for example. In your code, you are adding the LIST as LIST to another LIST. Then you wipe that list you added, you your final list only contains one EMPTY list. – GhostCat Jul 01 '16 at 12:27
  • 1
    What if you do: `aList1.add((ArrayList) aList2.clone());` – Kartic Jul 01 '16 at 12:30
  • +1 on the clone function. ArrayList implements Cloneable https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html – Rian O'Dwyer Jul 01 '16 at 12:34
  • I would prefer `List1.add(new ArrayList<>(aList2));` over clone – user140547 Jul 01 '16 at 12:35
  • @Kartic that's also a good one – user6447244 Jul 01 '16 at 12:42
  • I guess if your list holds only String, clone would be faster, but it's absolutely your choice. – Kartic Jul 01 '16 at 12:46

3 Answers3

0

Here is a description step by step of your code:

// Define a list of lists named aList1 
ArrayList<ArrayList<String>> aList1 = new ArrayList<>();

// Define a list named aList2
ArrayList<String> aList2 = new ArrayList<>();

// Add an element to aList2
aList2.add("Object 1");

// Add an element to aList2
aList2.add("Object 2");


// Add aList2 to aList1
aList1.add(aList2);

// Empty aList2
aList2.clear();

// Print aList1. Now aList1 is not empty. Is a list containing just
// one list of size 0.
System.out.println(aList1);

As shown step by step. At the end of the process aList1 is not empty. It contains just one empty list.

Clearing aList2 remove all elements inside aList2, because aList2 is the same list in position 0 of aList1 at the end aList1.get(0) exists and is the empty list pointed also by aList2.

Davide Lorenzo MARINO
  • 26,420
  • 4
  • 39
  • 56
0

Simply Create new ArrayList From the ArrayList you want to add.

Here is it

   ArrayList<ArrayList<String>> aList1 = new ArrayList<>();
   ArrayList<String> aList2 = new ArrayList<>();
   aList2.add("Object 1");
   aList2.add("Object 2");
   aList1.add(new ArrayList(aList2));
   aList2.clear();
   System.out.println(aList1);
Ahmed Amr
  • 579
  • 3
  • 10
0

consider using a list for coping as backup the list you are clearing..

Example:

public static void main(String[] args) {
    final List<List<String>> aList1 = new ArrayList<List<String>>();
    final List<String> aList2 = new ArrayList<String>();
    final List<String> aList3 = new ArrayList<String>();

    aList2.add("Object 1");
    aList2.add("Object 2");
    aList3.addAll(aList2);
    aList1.add(aList2);
    aList1.add(aList3);
    aList2.clear();
    System.out.println(aList1);
}
Community
  • 1
  • 1
ΦXocę 웃 Пepeúpa ツ
  • 47,427
  • 17
  • 69
  • 97