• Post Reply Bookmark Topic Watch Topic
  • New Topic

toArray method for own ArrayList implementation  RSS feed

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all, I am trying to write my own implementation for ArrayList<E> and I don't seem to understand how to do my toArray method and why exactly what I did doesnt work.



When I test it like this it gives me a
java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to...

Obviously I am lacking knowledge here, so can someone suggest how to fix this and possibly give me a hing what's wrong.
Thanks!
 
Bartender
Posts: 1840
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I believe this is the line of code it is complaining about.
It is saying it can't turn an Object[] into an E[]

Is there a reason you are creating an Object[] ?
 
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's one of the issues with generics. The type isn't available to actually allow instantiation of an object of that type. Or instantiation of an array of that type.

Henry
 
Stefan Evans
Bartender
Posts: 1840
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So a call to Array.newInstance() would be appropriate? see java.lang.reflect.Array
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Disagree.
What you want to return is an array containing the elements from 0..size (inc‑exc). I think something like Arrays#copyOf←plain text changed to link as “edit” would be better. That allows you to create a new array of the right size (and the right type with generics) and return it. That is course does not constitute a defensive copy. Because you are not returning the backing array you are unable to make structural alterations to the List, but if the individual elements are mutable, their state can be altered by outside code. But no other List implementation offers a proper defensive copy, so I don't think that is a problem.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reflection Array class won't work here, as it needs a Class instance to determine the element type. Or a sample element instance that can be used to get the element Class instance.

The Arrays class won't work here either, as there isn't an array to copy. This compile error is with the constructor.

So, the options are to change the constructor with enough information to create the E array or to just use an Object array instead (including changing the return from the toArray method). I recommend the later.

Henry
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for the delay in replying. I have had a look at your code and I think it can be done.Unfortunately I have only managed to get it to return Object[] and you can't seem to cast that to String[]. You can see that its runtime type is Object[] if you execute my code. I am not sure whether you can create a String[] from my code at all.
You can however create an Object[] whose elements are Strings, and you can see from the successful cast and use of a specific String method that their runtime type is String.

By the way: Don't permit a 0 capacity. Make your minimum capacity 1.
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Look at the List#toArray() method. If you want a particular type of array returned you have to pass an instance of that array as an argument. Not sure how that is implemented in java.util.ArrayList however. You can find out because there is a .zip file with all the source code in your Java® installation folder.
 
Werner Holt
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Look at the List#toArray() method. If you want a particular type of array returned you have to pass an instance of that array as an argument. Not sure how that is implemented in java.util.ArrayList however. You can find out because there is a .zip file with all the source code in your Java® installation folder.

Thanks a lot, really!
 
Campbell Ritchie
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!