0
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[]

raj
  • 101
  • 1
  • 3
  • 11
  • 1
    Read https://stackoverflow.com/questions/2770321/what-is-a-raw-type-and-why-shouldnt-we-use-it – JB Nizet Oct 15 '17 at 07: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 Oct 15 '17 at 08:14

1 Answers1

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];
rkosegi
  • 14,165
  • 5
  • 50
  • 83
  • But it is giving a warning unchecked conversion required List[] found: ArrayList[] – raj Oct 15 '17 at 08:53
  • 1
    yes, `new ArrayList[2]` will not compile. You should reconsider you data structure. Currently it seems pretty horrible to work with. – rkosegi Oct 15 '17 at 08:57