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

2
  • 1
    Read stackoverflow.com/questions/2770321/… Commented 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. Commented Oct 15, 2017 at 8:14

1 Answer 1

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];
Sign up to request clarification or add additional context in comments.

2 Comments

But it is giving a warning unchecked conversion required List<Integer>[] found: ArrayList[]
yes, new ArrayList<Integer>[2] will not compile. You should reconsider you data structure. Currently it seems pretty horrible to work with.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.