aspose file tools*
The moose likes Java in General and the fly likes Cast Object to Object[] Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Cast Object to Object[]" Watch "Cast Object to Object[]" New topic
Author

Cast Object to Object[]

Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
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.
Mohamed Sanaulla
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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>


Mohamed Sanaulla | My Blog
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4391
    
    8

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

Joined: Dec 21, 2009
Posts: 102
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

Joined: Apr 06, 2010
Posts: 4391
    
    8

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Joined: Dec 21, 2009
Posts: 102
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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

You can cast it to corresponding primitive.



I dont know if this would be help:
Theodore David Williams
Ranch Hand

Joined: Dec 21, 2009
Posts: 102
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

Joined: Apr 06, 2010
Posts: 4391
    
    8

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
Saloon Keeper

Joined: Sep 08, 2007
Posts: 3071
    
  33

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

Joined: Dec 21, 2009
Posts: 102
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

Joined: Apr 06, 2010
Posts: 4391
    
    8

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

Joined: Dec 21, 2009
Posts: 102
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!!!
Yuriy Fuksenko
Ranch Hand

Joined: Feb 02, 2001
Posts: 413
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:

>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Cast Object to Object[]