7

In my java code, I try to build a list of arraylist, my code is as follows,

private ArrayList<Integer>[] listoflist;
listoflist = (ArrayList<Integer>[]) new Object[875715];

However, when I compile the code, the compiler keeps saying that

[Ljava.lang.Object; cannot be cast to [Ljava.util.ArrayList;

Can I ask why I can not cast Object[] to ArrayList[]?

3
  • 8
    Why would you expect to be able to? Commented Mar 9, 2013 at 19:14
  • 4
    Because you're trying to cast a non-parameterized type : Object, to a parameterized type : ArrayList<Integer>. Commented Mar 9, 2013 at 19:16
  • And what do you expect to do? Commented Mar 9, 2013 at 19:17

4 Answers 4

4

You said that you're trying to build a list of ArrayLists. But... you're trying to use an array to do that... Why not just use another ArrayList? It's actually pretty easy:

private List<List<Integer>> listoflist = new ArrayList<ArrayList<Integer>>();

Here's an example of using it:

ArrayList<Integer> list1 = new ArrayList<Integer>();
list1.add(Integer.valueOf(3));
list1.add(Integer.valueOf(4));
ArrayList<Integer> list2 = new ArrayList<Integer>();
list2.add(Integer.valueOf(6));
list2.add(Integer.valueOf(7));
listoflist.add(list1);
listoflist.add(list2);

Saying ArrayList<ArrayList<Integer>> so many times is kinda weird, so in Java 7 the construction can just be new ArrayList<>(); (it infers the type from the variable you're assigning it to).

Sign up to request clarification or add additional context in comments.

Comments

4

Java is a strong typed language - hence you cannot simply cast one type to the other. However you can convert them.

In case of Object[] to List simply use

Object[] arr = new Object[]{...};
List<Object> list = Arrays.asList(arr);

and if you want to use it as an ArrayList, e.g. if you want to add some other elements, simply wrap it again

 ArrayList<Object> arrList = new ArrayList<Object>(Arrays.asList(arr));

Comments

1

You can make an n-dimensional ArrayList, just like an n-dimensionaly Array, by putting ArrayLists into ArrayLists.

Here an example with 3 dimensions to show the concept.

public static void main(String args[]){
    ArrayList<ArrayList<ArrayList<Integer>>> listOfListOfList = new ArrayList<ArrayList<ArrayList<Integer>>>();
    int firstDimensionSize = 3;
    int secondDimensionSize = 4;
    int thirdDimensionSize = 5;

    for (int i = 0; i < firstDimensionSize; i++) {
        listOfListOfList.add(new ArrayList<ArrayList<Integer>>(vertices));
        for (int j = 0; j < secondDimensionSize; j++) {
            listOfListOfList.get(i).add(new ArrayList<Integer>());
            for(int k = 0; k < thirdDimensionSize; k++) {
                listOfListOfList.get(i).get(j).add(k);
            }
        }
    }
}

Note that you can leave the <> empty after the new ArrayList<>. Java will infer the type (no matter how nested), since java 7 I believe. I just wrote them down in the example to show what type you are handling at every level, to make the example more clear. You can still write them down to make your code more readable.

Comments

-2

define it in single line like following, compiler doesn't complain

private ArrayList[] listoflist = (ArrayList<Integer>[]) new Object[10];

2 Comments

You're wrong, this is exactly the same statement but written differently, with the same warning : Type safety: Unchecked cast from Object[] to ArrayList<Integer>[], and the same error : [Ljava.lang.Object; cannot be cast to [Ljava.util.ArrayList;
@Rob: understood. But when I tried to do it eclipse then it was giving error. But after single line, it is giving warning which you mentioned.

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.