0

Using the AWT.Transferable, I get an argument "data: Any" which is actually of type java.util.Arrays.ArrayList. How can I cast the "data" explicitly to this type? It seems I do not have any access to the "ArrayList"-Type itself...

1
  • IIRC ArrayList in Java are always untyped (i.e ArrayList<Object> or ArrayList[Any] in Scala), you have to cast each element individually. Commented Nov 18, 2010 at 12:00

2 Answers 2

1

This

data match {
  case jlist: java.util.List[_] => // I got an java.util.list!
  case _ => // oops, unexpected!
}

is type-safe, which doesn't happen to be the case of asInstanceOf.

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

Comments

1

Why do you need the class explicitly? Couldn't you use the java.util.List interface?

data.asInstanceOf[java.util.List[_]]

Note that casting is not encouraged, which is why it looks so awful in Scala!

1 Comment

Ah, that did the trick. Should just have checked the interfaces of ArrayList... Thanks!

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.