• Post Reply Bookmark Topic Watch Topic
  • New Topic

Casting arrays  RSS feed

 
Jamie CB
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an Object[] of integers. Im trying to get an Integer[].

Integer[] keys = (Integer) getArray() gives me "Inconvertible types" when compiled.

Integer[] keys = (Integer[]) getArray() gives me a ClassCastException during runtime.

What am I doing wrong? Can I even convert them that simply or do I have to iterate through?

Thanks

Jamie
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are thinking that an array has an array hierarchy above it. This is not so: an Integer array, for instance, has only Object above it. (Also, every array IS-A Cloneable and java.io.Serializable.)

I think you will have to loop through the Object array and cast the individual elements to Integer. Alternatively, do you control the code so that you can avoid the need to manipulate an Object array and deal only with an Integer array.
 
Jolie Lee
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, do u mean something like this?

int c[] = new int[12];
 
Roger Chung-Wee
Ranch Hand
Posts: 1683
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, examine your code and see why an Object array is needed. If it isn't, and if you are able to create and use an array of the correct type, then do so.
 
Jeffrey Spaulding
Ranch Hand
Posts: 149
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah that puzzled me a lot in the beginning, but you get used
to the weirdest stuff over time

I always found that clumsy and awkward.

Loop is the only way to do it.

From what i hear in Java5 you have a first step in this direction
but that's mainly targeted towards Collections and autoboxing of
primitive vars to their Classes i.e. (int --to-- Int)

Keywords are "Generics" and "Autoboxing"


J.
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the declared type is an Object[], there's not much you can do about it. Of coure, you could change the declared type, but you might not have control over that.

You could try the following:



but this isn't necessarily as clear to the reader as looping through the original array.
[ May 10, 2005: Message edited by: Joel McNary ]
 
Jamie CB
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The Object[] is coming from a JList, so I cant really change it. Ill just loop through it instead. Thanks for clearing that up.

Jamie
 
Steven Bell
Ranch Hand
Posts: 1071
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The problem is that the original object was created as type Object[].

This is why in the classes like ArrayList you have two toArray methods. One that takes no arguments and returns an Object[] and one that takes an Object[] (which is meant to be a subclass such as Integer[]) and uses reflection to return the correct type of array so that you can cast it.
[ May 10, 2005: Message edited by: Steven Bell ]
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
BTW,
Jamie CB, welcome to JavaRanch!

Please take the time to read the JavaRanch Naming Policy, and then change your display name to comply. (We are looking for real-sounding first and last names; while initials are permitted as first names, they are not as last names).

Thanks! and welcome to the JavaRanch!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!