Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

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

share|improve this question
8  
Why would you expect to be able to? – Jon Skeet Mar 9 '13 at 19:14
4  
Because you're trying to cast a non-parameterized type : Object, to a parameterized type : ArrayList<Integer>. – Rob Mar 9 '13 at 19:16
    
And what do you expect to do? – Amir Pashazadeh Mar 9 '13 at 19:17
    
Thank you Rob, make sense – Bpache Mar 9 '13 at 20:23
up vote 4 down vote accepted

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).

share|improve this answer
    
thank you, great answer! – Bpache Mar 9 '13 at 20:21

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));
share|improve this answer
    
thank you very much. – Bpache Mar 9 '13 at 20:18

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.

share|improve this answer

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

private ArrayList[] listoflist = (ArrayList<Integer>[]) new Object[10];
share|improve this answer
2  
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 Mar 9 '13 at 19:33
    
@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. – AmitG Mar 9 '13 at 19:36

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.