Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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 having a java.lang.ClassCastException: java.util.Arrays$ArrayList cannot be cast to java.util.ArrayList

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

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

share|improve this question

3 Answers 3

up vote 5 down vote accepted

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()); 

or:

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

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

share|improve this answer
1  
Why not assign it to a List directly?. –  TheLostMind Mar 4 at 10:15
    
@AlexisC.: Sorry, meant to do that :) –  Jon Skeet Mar 4 at 10:21
    
@TheLostMind: Not sure what you mean - the declared type of list? –  Jon Skeet Mar 4 at 10:21
    
You've added it - List<Foo> list = new ArrayList<>(Arrays.asList(sos1.getValue()); :) –  TheLostMind Mar 4 at 10:23
1  
@TheLostMind: Righto. –  Jon Skeet Mar 4 at 10:23

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()));
share|improve this answer
    
how can I fix it then? –  SaintLike Mar 4 at 10:11
    
@SaintLike see edit –  Eran Mar 4 at 10:13

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.

share|improve this answer

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.