• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Convert LinkedList<Object> to LinkedList<T>

 
Ranch Hand
Posts: 64
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an Object Array, which I have converted to a LinkedList of Objects.

I need to convert the Object datatype to another data type (lets say T)


LinkedList<T> newList = ((LinkedList<T>) Arrays.asList( oldList[] ));


This tells me "Cannot convert from List<Object> to LinkedList<T>

anyone know a working way (preferably preference efficient) to convert the data type of the List?
 
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You want to store the elements of an Object[] into a LinkedList<T>. So every element of the array must a T or a child of T. Since there is no way to prove this for the entire array (and I'm assuming that the array not just happens to be an T[]) you'll need to cast and add every element to the LinkedList.
 
Lance Colton
Ranch Hand
Posts: 64
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Wouter Oet wrote:You want to store the elements of an Object[] into a LinkedList<T>. So every element of the array must a T or a child of T. Since there is no way to prove this for the entire array (and I'm assuming that the array not just happens to be an T[]) you'll need to cast and add every element to the LinkedList.



Ugh, I was afraid of that. Thank you for the reply
 
Master Rancher
Posts: 5161
83
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually it is possible to do what you want. Or it would be, if you didn't have a bigger problem: you're trying to cast to a LinkedList, but you don't have a LinkedList. Arrays.asList() doesn't create a LinkedList, and you have done nothing else that could create a LinkedList here. So there's no way to make that cast succeed.

However if we replace LinkedList with a plain List, that problem goes away, and we can get to what you apparently wanted to ask about. Yes, there is a way:

The key is the double cast on line 4. The cast to raw List causes the compiler to forget the generic parameter <Object> entirely. And the cast to List<String> tells the compiler it has a new generic parameter, <String>. Casting to and from raw types would generate a warning, so using @SuppressWarnings is nice to tell the compiler to shut up about that.

Note that this sort of thing is risky. You are telling the compiler to shut up, because you know what you're doing. That's good, if you actually do know what you're doing. But it can give you confusing results later if it turns out you were wrong.

For fun, try predicting what will happen with the following code. Will it compile? Will it run? Exactly what line will the error (compilation error or runtime exception) occur on? Why?

After trying to decide in advance what the answer is, try it and see. Compile it and run it (if possible), and see what happens. Was it what you expected?
 
Lance Colton
Ranch Hand
Posts: 64
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:
The key is the double cast on line 4. The cast to raw List causes the compiler to forget the generic parameter <Object> entirely. And the cast to List<String> tells the compiler it has a new generic parameter, <String>. Casting to and from raw types would generate a warning, so using @SuppressWarnings is nice to tell the compiler to shut up about that.



That is really cool, I had no idea you could do that thank you

Mike Simmons wrote:
For fun, try predicting what will happen with the following code. Will it compile? Will it run? Exactly what line will the error (compilation error or runtime exception) occur on? Why?

After trying to decide in advance what the answer is, try it and see. Compile it and run it (if possible), and see what happens. Was it what you expected?



Sound's fun, I predict the program will fail at the for loop it will throw a CastException. (will post results)
 
Lance Colton
Ranch Hand
Posts: 64
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


lscolton@Workstation:~/Desktop$ javac Main.java
lscolton@Workstation:~/Desktop$ java Main
[one, two, three, 4, 5, 6]
one
two
three
Exception in thread "main" java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.String
at Main.main(Main.java:14)



So close >.< doesn't fail in the foor loop until it gets to the int's, I thought It would fail before it started iterating.
 
Sheriff
Posts: 22849
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would it? The for-each loop simply takes each elements, casts that to String, and then uses it. It's this cast that fails, but only for the Integers, not for the Strings.
 
Lance Colton
Ranch Hand
Posts: 64
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I thought about it more I probably realized that, however for some reason thought the failure would happen as soon as you attempted to access the list
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic