import java.util.ArrayList;
import java.util.*;
import java.util.List;
class A
{
public static void main(String args[])
{
ArrayList arrayOfArrayList[]=new ArrayList[2];
int i;
for(i=0;i<2;i++)
{
arrayOfArrayList[i] = new ArrayList<Integer>();
}
arrayOfArrayList[0].add(1);
arrayOfArrayList[0].add(2);
arrayOfArrayList[1].add(3);
arrayOfArrayList[1].add(4);
Integer arr[][] = new Integer[2][];
arr[0] = arrayOfArrayList[0].toArray(arr[0]);
arr[1] = arrayOfArrayList[1].toArray(arr[1]);
for (Integer x : arr[0])
System.out.print(x + " ");
}
}
I am trying to create an array of arrayLists and later convert it to array. But the compile time error error: Incompatible type conversion object[] to Integer[]
-
1Read stackoverflow.com/questions/2770321/…JB Nizet– JB Nizet2017年10月15日 07:57:11 +00:00Commented Oct 15, 2017 at 7:57
-
In addition to avoiding raw types you will have to change toArray(arr[0]) to toArray(new Integer[0])) to avoid a NullPointerException.cpp beginner– cpp beginner2017年10月15日 08:14:41 +00:00Commented Oct 15, 2017 at 8:14
1 Answer 1
Here you created raw type :
ArrayList arrayOfArrayList[]=new ArrayList[2];
If you need to store Integer there, you should use correct generics:
ArrayList<Integer> arrayOfArrayList[]=new ArrayList[2];
answered Oct 15, 2017 at 7:57
rkosegi
14.8k5 gold badges58 silver badges90 bronze badges
Sign up to request clarification or add additional context in comments.
lang-java