java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList
Asked Answered
G

7

96

Can you explain me why does this happen and how can I fix it please?

So I'm using Oracle-ADF and I'm using shuttle components. I get the selected values using the sos1.getValue();

The getValue() method returns an object and I'm trying to convert it to an ArrayList so I can work with it later. Therefore I've created the ArrayList sos1Value

However, this line of code is going bananas:

sos1Value = (ArrayList) Arrays.asList(sos1.getValue());

And I keep getting java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

I've tried other ways like: sos1Value = (ArrayList) sos1.getValue();

But I keep having the same problem, what can I do?

Goatfish answered 4/3, 2015 at 10:8 Comment(0)
I
144

Arrays.asList returns a List implementation, but it's not a java.util.ArrayList. It happens to have a classname of ArrayList, but that's a nested class within Arrays - a completely different type from java.util.ArrayList.

If you need a java.util.ArrayList, you can just create a copy:

ArrayList<Foo> list = new ArrayList<>(Arrays.asList(sos1.getValue()); 

If you don't need an ArrayList just remove the cast:

List<Foo> list = Arrays.asList(sos1.getValue()); 

(if you don't need any members exposed just by ArrayList).

Intolerance answered 4/3, 2015 at 10:11 Comment(9)
Why not assign it to a List directly?.Interdependent
@AlexisC.: Sorry, meant to do that :)Intolerance
@TheLostMind: Not sure what you mean - the declared type of list?Intolerance
You've added it - List<Foo> list = new ArrayList<>(Arrays.asList(sos1.getValue()); :)Interdependent
@TheLostMind: Righto.Intolerance
new ArrayList<String>( Arrays.asList(new String[] { "hey", "yo"})) would also workSpiros
@AbSin The inner new String[] { } is redundant. The asList(String...) method implicitly allocates such a string array, without you needing to explicitly spell it out.Blasted
@JonSkeet What's the motivation behind having a separate ArrayList class? Why couldn't the implementation do something like return Collections.unmodifiableList(new ArrayList(array));?Blasted
@Alexander: That would copy the array, rather than providing a view over it. They behave very differently.Intolerance
F
9

Arrays.asList(sos1.getValue()); produces an instance of a List implementation (java.util.Arrays$ArrayList) that is not java.util.ArrayList. Therefore you can't cast it to java.util.ArrayList.

If you change the type of sos1Value to List, you won't need this cast.

If you must have an instance of java.util.ArrayList, you can create it yourself :

sos1Value = new ArrayList (Arrays.asList(sos1.getValue()));
Fiddling answered 4/3, 2015 at 10:10 Comment(1)
how can I fix it then?Goatfish
I
8

The ArrayList returned by Arrays.asList() method is NOT java.util.ArrayList it is a static inner class inside Arrays class. So, you can't cast it to java.util.ArrayList.

Try converting / assigning it to a List.

Interdependent answered 4/3, 2015 at 10:10 Comment(0)
S
1

The easy way (not efficient) is

ArrayList sos2Value = new ArrayList()
sos2Value.addAll(Arrays.asList(sos1.getValue()))

sos2Value is java.util.ArrayList.
Arrays.asList(sos1.getValue()) is java.util.Arrays$ArrayList

But I'm not clear what you want to do.

Semifluid answered 22/10, 2021 at 6:23 Comment(0)
D
0

I used it in this way:

 private fun getArtists(): ArrayList<ArtistItem> {
        var xp = myDb.daoNote().getArtists() as ArrayList<ArtistItem>
        val x: List<ArtistItem> =  xp.sortedWith(compareBy { it.isBookmarked})
        var pp = ArrayList<ArtistItem>()
        for(obj in x)
        {
            pp.add(obj)
        }

     return pp
    }
Doura answered 17/5, 2022 at 5:54 Comment(0)
W
0

If you are working on Eclipse IDE then just make a your package and under that package create java file,it will work.

Winegrower answered 7/12, 2023 at 11:13 Comment(0)
L
-1

java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

You need to set the type to java.util.List

Legitimize answered 1/6, 2021 at 8:33 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.