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.

6
  • 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. Commented Jul 1, 2016 at 12:27
  • 1
    What if you do: aList1.add((ArrayList<String>) aList2.clone()); Commented Jul 1, 2016 at 12:30
  • +1 on the clone function. ArrayList implements Cloneable docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html Commented Jul 1, 2016 at 12:34
  • I would prefer List1.add(new ArrayList<>(aList2)); over clone Commented Jul 1, 2016 at 12:35
  • @Kartic that's also a good one👍🏼 Commented Jul 1, 2016 at 12:42

3 Answers 3

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);
Sign up to request clarification or add additional context in comments.

1 Comment

You're the man! Thanks, that was exactly what I needed!!
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.

Comments

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);
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.