I am writing a method which converts Integer
data type from a List
of List
s, to a primitive array of arrays, type int[][]
.
Question
Is there any way to convert Integer
to int
using Java API?
For your information, int[] set
will be used for other purposes so please ignore.
What I have found
Apache Commons API "toPrimitive" method converts Integer to int type. However, I'm only looking for solutions using native java API.
This is my code so far
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;
}
}
Expected results
List entry: [[1,2],[2],[3]]
return: {{1,2},{2},{3}}
Errors
tempArray[i][j] = ll.get(j);
returns java.util.List<java.lang.Integer> cannot be converted to int
.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…