• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

Cast Object to Object[]

 
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an old api that returns a big O Object that is really an array of primitive types. I would like to get that Object into an Object array so that I can convert it to a List<Object> such that I can call methods like subList(....) on it, to get a sub set of the array. However I cannot convert the Object to an Object array.




On the convert I get:


Exception in thread "main" java.lang.ClassCastException: [I
at ArrayTest.main(ArrayTest.java:7)



Is there anything I can do? I cannot change the api I am using so I am stuck with this.
 
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you have is an int array- Array of primitives. How can you typecast it to Array of Instances? You can use an Integer[] or List<Integer>
 
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Primitive arrays aren't object arrays, so a straight cast won't work.

I'd suggest: leave it as an int[], then use Arrays.asList():
If you want to use generics, I think you might need to create a List<Integer> and iterate through your array.
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can do this either:


Same error even with (Integer[] cast instead of Object[]). Besides the api I am using could return an int[], byte[], double[], float[], etc....

If I do this



What I get is a list of arrays (in my case a list with on element and that element is an array of primitive ints), not a list of my integer elements in the array. I do not really want to loop through the entire array and store each element in a Generic Collection because this array of data could be huge and would take to long.

Thanks for the ideas guys.....any more???
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Theodore David Williams wrote:If I do thisWhat I get is a list of arrays


That's because you're using an Object reference type. The reference type needs to be int[].
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Theodore David Williams wrote:You can do this either:

Same error even with (Integer[] cast instead of Object[]). Besides the api I am using could return an int[], byte[], double[], float[], etc....

What I get is a list of arrays (in my case a list with on element and that element is an array of primitive ints), not a list of my integer elements in the array. I do not really want to loop through the entire array and store each element in a Generic Collection because this array of data could be huge and would take to long.

Thanks for the ideas guys.....any more???



The Casting you are doing is not correct. Also its always better to avoid Typecasting. If at all you want to typecast its better to have an instance of check before typcasting. This way you can avoid ClassCastException.


You can use Generics- ArrayList<T> where T can be- Integer, Double, Boolean ...

You can then store your elements in this list instead of putting them in an array then typecasting it and then again creating List<Object>.
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok let me go back to square one.

I am using an api (I cannot change this api to return a generic list/collection) that is returning an array of primitives. I.E. int[], byte[], double[], etc......

My example code was just that a simple example, please ignore the fact that I am doing this:

All I was really trying to show is that I am stuck with this Object that is an array. I CANNOT change this line it is a quick example that the API I am using returns an Object.

So the real code would look something like this


So this API always, always, always returns an Object, which is really and array of primitives.

I would really like to get this Object cast into a List but do not think that I can unless I iterate over the entire thing.

So using that code above how can I get 'Object data' cast into an Object[] or a List<Object>???
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can cast it to corresponding primitive.



I dont know if this would be help:
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah ok I was hoping that was not my only option: because then I have:

if (int[])
else if(double[])
else if(float[])
else if(byte[])
else if(char[])
and on and on and on........
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do what Mohamed said. But, since you say it can return a bunch of any primitives, you're going to have to check them. This should work.


Edit...ah, you've already worked that out! I'm not sure you're going to get anything easier than that, though. At least you only have to do it once - if this is common then write a wrapper round your API that returns something useful.
 
Mohamed Sanaulla
Bartender
Posts: 3225
34
IntelliJ IDE Oracle Spring Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Theodore David Williams wrote:Yeah ok I was hoping that was not my only option: because then I have:

if (int[])
else if(double[])
else if(float[])
else if(byte[])
else if(char[])
and on and on and on........



I gave int[] as an example. In any case you cannot cast it to Object[]. See if you can get some other methods from the other people on the forum.
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matthew just as an aside the following does not work as intended:


Object data = oldUselessApi.getData();
List list;
if (data instanceof double[]) {
list = Arrays.asList((double[])data);
}
else if (data instanceof float[]) {
list = Arrays.asList((float[])data);
}
else if...



That still gives you a list with ONE element. And that element is an array. You do not in fact get a list that contains each element in the array.

I.E.



This does not print out 1 2 3 4 5.

It prints: '[I@8fce95'
 
Matthew Brown
Bartender
Posts: 4568
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Sorry about wasting your time there - I must admit I didn't try it out in advance. It looks as though it's to do with the way variable arguments and autoboxing interact. asList is declared as taking a T... argument, and I didn't realise it would always interpret a primitive array as a single object rather than boxing the arguments.

(Having now tried it out I've confirmed that an Object array, e.g. String[], works as I expected - you get different behaviour depending on whether you add the cast or not).

In that case, unfortunately, I think you're going to have to iterate over the array. Even with a huge array that's going to be pretty quick as long as you either use a LinkedList, or use an ArrayList with the capacity initialised to the right size. Or iterate to convert to an Integer[] (etc) and then use asList.
 
Theodore David Williams
Ranch Hand
Posts: 102
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Matthew, please no need for apologies I really appreciate the fact that you jumped in and helped, after all that what this is all about.

You are right I cannot avoid iterating

Anyway thanks again for the help!!!
 
Ranch Hand
Posts: 413
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't know if this helps you in any way, and it is definitely not the best coding style, but the following would work for any type of primitive array except boolean and char:

>
 
The longest recorded flight time of a chicken is 13 seconds. But that was done without this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic