将int数组添加到2d ArrayList

时间:2021-12-28 21:37:22

Anyone know how to add an int[] array into a 2d ArrayList<ArrayList<Integer>>?

任何人都知道如何将int []数组添加到2d ArrayList >中?

The code...

代码...

import java.util.ArrayList;
import java.util.Arrays;

public class Test2 {
    public static void main(String[] args) {
        int[] intArray = {1,1,1,1};
        ArrayList<ArrayList<Integer>> list = new ArrayList<>();

        list.add(new ArrayList<Integer>(Arrays.asList(intArray))); // <- compile error
    }
}

2 个解决方案

#1


7  

change your int array to an Integer array.

将int数组更改为Integer数组。

Integer [] intArray = {1,1,1,1};

#2


1  

Make your ArrayList an ArrayList of int[]:

使您的ArrayList成为int []的ArrayList:

ArrayList<int[]> list = new ArrayList<>();
list.add(intArray); // should work now

#1


7  

change your int array to an Integer array.

将int数组更改为Integer数组。

Integer [] intArray = {1,1,1,1};

#2


1  

Make your ArrayList an ArrayList of int[]:

使您的ArrayList成为int []的ArrayList:

ArrayList<int[]> list = new ArrayList<>();
list.add(intArray); // should work now