4

I have been trying out nested arrayList methods. I have a problem.

public static void main(String[] args) {
    List<List<Integer>> list = new ArrayList<>();
    List<Integer> subList = new ArrayList<Integer>(){{
        add(1);
        add(2);
    }};
    list.add(subList);
    subList.clear();
    subList.add(3);
    subList.add(4);
    list.add(subList);
    System.out.println(list);
}

This does not give expected output. The result is

[[3, 4], [3, 4]]

and not [[1, 2], [3, 4]]

What is wrong with my Code.

EDIT: I have a few more issues on the code. Should I create a new quetsion or add it here.

3
  • Please add the proper tag. Commented May 11, 2019 at 15:11
  • 5
    You are adding just a reference of sublist to list. You need to create a new sublist instead of clearing the old one. Commented May 11, 2019 at 15:13
  • What you have created: list = [ sublist, sublist ] Note that you have only one sublist with two references to it. Commented May 11, 2019 at 15:19

1 Answer 1

12

I would advice you against using double brace initialization new ArrayList<Integer>{{ }} as it is kind of an anti-pattern and also creates anonymous inner classes.

As for the error, you should initialize a new list instead of clear()

public static void main(String[] args) {
    List<List<Integer>> list = new ArrayList<>();
    List<Integer> subList = new ArrayList<>();
    subList.add(1);
    subList.add(2);
    list.add(subList);
    subList = new ArrayList<>();
    subList.add(3);
    subList.add(4);
    list.add(subList);
    System.out.println(list);
}

Calling clear does not change the reference to the ArrayList, and will point to the same object. You should create a new reference by calling subList = new ArrayList<>()

A good read about the ill-effects of double brace initialization

https://blog.jooq.org/2014/12/08/dont-be-clever-the-double-curly-braces-anti-pattern/

EDIT: As @NoDataFound suggests, if you are using Java 8+, the process of creating a List can be much more simplified.

If you are using Java 8, you can create a list by using

List<Integer> subList = Arrays.asList(1, 2);

If you are using Java 9+, you can make use of

List<Integer> subList = List.of(1, 2);
Sign up to request clarification or add additional context in comments.

5 Comments

I couldn't agree more about the double-brace approach. People who use it generally do not understand what it really means, because people who do understand what it really means generally avoid it. It is an abomination. And of course you're right about the nature of the OP's problem, too.
You should add that calling new ArrayList + add can be totally replaced by Arrays.asList(1, 2) and Arrays.asList(3, 4), or in Java 9++, List.of(1, 2) and List.of(3, 4).
Yes @JohnBollinger. That's why I thought I should add that point too, even though it seems unrelated to the actual problem.
@NoDataFound True. Will add.
tysm @BlackPearl..!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.