转换List <list <integer>&gt; to Array <array <int>&gt;

时间:2016-08-19 02:27:01

标签: java arrays

我正在编写一个方法,将Integer数据类型从List列表转换为“array of array”的基本类型“int”。

问题

有没有办法使用Java API将Integer转换为int?

有关您的信息,int[] set将用于其他目的,请忽略。

我找到的内容

Apache Commons API“toPrimitive”方法将Integer转换为int类型。但是,我只是在寻找使用本机java API的解决方案。

这是我目前的代码

class Test {
int[][] convert(int[] set) {
    List<List<Integer>> ll = new ArrayList<List<Integer>>();
    ll.add(new ArrayList<Integer>());
    ll.add(new ArrayList<Integer>());
    ll.add(new ArrayList<Integer>());
    ll.get(0).add(1);
    ll.get(0).add(2);
    ll.get(1).add(2);
    ll.get(2).add(3);

    System.out.println(ll + " " + ll.size());

    int[][] tempArray = new int[0][0];
    for (int i = 0; i < ll.size(); i++) {
        for (int j = 0; j < ll.get(i).size(); j++) {
            tempArray[i][j] = ll.get(j);
        }
    }
    return tempArray;
}

预期结果

List entry: [[1,2],[2],[3]]
return: {{1,2},{2},{3}}

错误

tempArray[i][j] = ll.get(j);

返回java.util.List<java.lang.Integer> cannot be converted to int

请原谅我错误的问题/代码格式。我是stackoverflow&amp;的新手。一个Java语言的初学者所以请纠正我。

5 个答案:

答案 0 :(得分:3)

要回答您的确切问题,是的Integer可以使用int方法转换为intValue(),或者您可以使用自动装箱转换为int }。

所以循环的最里面部分可以是以下任何一个:

tempArray[i][j] = ll.get(i).get(j).intValue();
tempArray[i][j] = ll.get(i).get(j);

但是,我们也可以采取不同的策略。

作为this answer对类似问题的修改,在Java 8中,您可以使用Streams映射到整数数组。这种结构只需要一层额外的映射。

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

int[][] arr = list.stream()
    .map(l -> l.stream().mapToInt(Integer::intValue).toArray())
    .toArray(int[][]::new);

Ideone Demo

答案 1 :(得分:2)

145-<digits>

应该是

tempArray[i][j] = ll.get(j);

但是,您还有一些其他错误(需要声明数组);您还可以缩短初始化例程。像,

tempArray[i][j] = ll.get(i).get(j);

答案 2 :(得分:0)

Java 7。将 List<List<Integer>> 转换为 int[][]

// original list
List<List<Integer>> list = Arrays.asList(
        Arrays.asList(1, 2),
        Arrays.asList(2),
        Arrays.asList(3));
// initialize the array,
// specify only the number of rows
int[][] arr = new int[list.size()][];
// iterate through the array rows
for (int i = 0; i < arr.length; i++) {
    // initialize the row of the array,
    // specify the number of elements
    arr[i] = new int[list.get(i).size()];
    // iterate through the elements of the row
    for (int j = 0; j < arr[i].length; j++) {
        // populate the array
        arr[i][j] = list.get(i).get(j);
    }
}
// output
System.out.println(Arrays.deepToString(arr));
// [[1, 2], [2], [3]]

答案 3 :(得分:0)

请阅读代码中的注释以更好地理解!

        public void someFn(){
         
        List<List<Integer>> outerList = new ArrayList<>();

       /* Outer list index is array row index[i] and inner list index is 
          array column index[j].  This is how we are going to parse and 
          add the data into our array */

        List<Integer> innerList = new ArrayList<>();
        innerList.add(1);  // ---> Index (0, 0) After adding inner list to outer list
        innerList.add(2);  // ---> Index (0, 1)
        innerList.add(3);  // ---> Index (0, 2)
        innerList.add(4);  // ---> Index (0, 3)

        List<Integer> innerList1 = new ArrayList<>();
        innerList1.add(5);  // ---> Index (1, 0)
        innerList1.add(6);  // ---> Index (1, 1)
        innerList1.add(7);  // ---> Index (1, 2)
        innerList1.add(8);  // ---> Index (1, 3)

        outerList.add(innerList);
        outerList.add(innerList1);

        parseArrayFromList(outerList);
}

     /* Outer list gives the number of rows size and the inner list 
        gives the number of coloumns size */
        private static void parseArrayFromList(List<List<Integer>> list){
      
            int arr[][] = new int[list.size()][list.get(0).size()];
    
            for(int i=0; i<list.size();i++){
                for(int j=0; j<list.get(i).size();j++){
                  arr[i][j] = list.get(i).get(j);
                }
            }
    }

答案 4 :(得分:-1)

我认为您不需要API将Integer转换为int。只是因为Integer本身有自己的方法。我们来看下面的例子

Integer integerObj = new Integer(100);
int intValue = integerObj.intValue();     // intValue = 100
相关问题