-1

I have the following List to keep 3x3 matrix values:

1 2 3 
4 5 6
7 8 9

List<List<Integer>> list = new ArrayList<>(); 

I want to add matrix values as shown below:

list.add(0, new ArrayList<Integer>{1, 2, 3});
list.add(1, new ArrayList<Integer>{4, 5, 6});
list.add(2, new ArrayList<Integer>{7, 8, 9});

But encounter syntax error. So, is it the correct way, or what is the proper way to assign matrix values by using cascade list?

  • Delete "0, ", "1, ", "2, " and it works. Or you want to use Dictionary> instead? Or was it not Dictionary in Java, something else? – BitLauncher Jul 05 '20 at 13:12
  • Would a multidimensional array be more suitable? – Sid Jul 05 '20 at 13:14
  • @BitLauncher I tried before asking the question, but did not worked. Do you mean `list.add(new ArrayList{1,2,3});`? I used like that. Any idea? –  Jul 05 '20 at 13:15
  • @Sid You right, but it is a question and I have to use List :( Any idea? –  Jul 05 '20 at 13:15
  • @Eklavya Why did you delete your post? –  Jul 05 '20 at 13:16

6 Answers6

2

There is no short form syntax to create an ArrayList and add items to it such as new ArrayList<Integer>{1, 2, 3}.

If you don't need to ArrayList, I would recommend using the immutable lists created by the List.of method:

list.add(0, List.of(1, 2, 3));
list.add(1, List.of(4, 5, 6));
list.add(2, List.of(7, 8, 9));

If you must use ArrayList for some reason, do it like this:

List<Integer> arrayList0 = new ArrayList<>();
arrayList0.add(1);
arrayList0.add(2);
arrayList0.add(3);

list.add(0, arrayList0);

// repeat for index 1, 2
Joni
  • 108,737
  • 14
  • 143
  • 193
2

You have the following options:

  1. Java-9 onwards:

    List<List<Integer>> list = new ArrayList<>();
    list.add(List.of(1, 2, 3));
    list.add(List.of(4, 5, 6));
    list.add(List.of(7, 8, 9));
    
  2. Before Java-9:

    List<List<Integer>> list = new ArrayList<>();
    list.add(Arrays.asList(1, 2, 3));
    list.add(Arrays.asList(4, 5, 6));
    list.add(Arrays.asList(7, 8, 9));
    
  3. List of Integer[]

        List<Integer[]> list = new ArrayList<>();
        list.add(new Integer[] { 1, 2, 3 });
        list.add(new Integer[] { 4, 5, 6 });
        list.add(new Integer[] { 7, 8, 9 });
    
  4. Using Stream API:

         List<List<Integer>> list = Stream.of(
                                             List.of(1, 2, 3), 
                                             List.of(4, 5, 6), 
                                             List.of(7, 8, 9))
                                         .collect(Collectors.toList());
    
Arvind Kumar Avinash
  • 71,965
  • 6
  • 74
  • 110
  • Nice answer. On the last one, you might want to do a `map(ArrayList::new)` since `List.of` is immutable. The OP may want to manipulate the values. – WJS Jul 05 '20 at 14:32
1

You cannot directly specify the element to add in the ArrayList constructor. Instead, you can do this.

List<List<Integer>> list = new ArrayList<>();
list.add(0, List.of(1, 2, 3));
list.add(1, List.of(4, 5, 6));
list.add(2, List.of(7, 8, 9));

Or

List<List<Integer>> list = new ArrayList<>();
list.add(0, Arrays.asList(1, 2, 3));
list.add(1, Arrays.asList(4, 5, 6));
list.add(2, Arrays.asList(7, 8, 9));

Note that List.of returns an immutable list. And Arrays.asList returns a list that can be set but not add.

0

Option 1:

    list.add(new ArrayList<>(Arrays.asList(1, 2, 3)));
    list.add(new ArrayList<>(Arrays.asList(4, 5, 6)));
    list.add(new ArrayList<>(Arrays.asList(7, 8, 9)));

Option 2:

    List<List<Integer>> list = new ArrayList<>();
    List<Integer> subList;
    int n = 1;

    for(int i = 0; i < 3; ++i){
        subList = new ArrayList<>();
        for (int j = 0; j < 3; ++j){
            subList.add(n);
            n++;
        }
        list.add(subList);
    }
Sercan
  • 2,081
  • 2
  • 10
  • 23
0

You can't initialize a java ArrayList like that. You can do it in the following ways:

  1. use Arrays.asList

    new ArrayList<>(Arrays.asList(1, 2, 3));

  2. if you use guava

    Lists.newArrayList(1, 2, 3)

  3. if you use Java8

    Stream.of(1, 2, 3).collect(Collectors.toCollection(ArrayList::new))

a.l.
  • 1,085
  • 12
  • 29
0

That's another solution, a bit complex but it works:

    List<List<Integer>> list = new ArrayList<>();
    ArrayList<Integer>row1 = new ArrayList();
    ArrayList<Integer>row2 = new ArrayList();
    ArrayList<Integer>row3 = new ArrayList();
    row1.addAll(List.of(1, 2, 3));
    row2.addAll(List.of(4, 5, 6));
    row3.addAll(List.of(7, 8, 9));
    
    list.add(0,row1);
    list.add(1,row2);
    list.add(2,row3);

    System.out.println(list);
Cardstdani
  • 4,999
  • 3
  • 12
  • 31