Search the archives!
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Typecasting Array Classes from Object[] to String[]
- From: boazbk@xxxxxxxxxxxxxx (Boaz Barak)
- Subject: Typecasting Array Classes from Object[] to String[]
- Date: Fri, 2 Oct 1998 02:29:39 +0200
You can redesign merge to instead of using new Object[] for its result method to use the java.lang.reflect.Array.newInstance() to create an array that is of a type more specific than Object[]. ( It can then use Array.set() to insert values in the array). This way the resulting Object[] will actually be a String[] and the call wil work. The questions that remain are: 1) How to get the component type of an array? The best way I know is to get the class name , remove the leading '[' , and use class.forName. Any better ideas? 2) Once you know the component type of the two arrays , what component type to choose for the result array? One simple way is to check using isAssignableFrom() if one of them is more specific than the other and if so use this type as the result type (meaning: if you have Shape[] and Square[] then Shape.class.isAssignableFrom(Square.class) returns true and the result array will be Square[]) . If this is not the case use Object[] Another way would be to try to find a most specific type that is assignable from both types. This might not be possible because of multiple interface inheritance, and so the options are: 1) define merge to take a third parameter which is the component type of the result array, throw an exception if it does not match. 2) try to cleverly choose a most appropriate class or interface considering either the types of the arrays or the types of the actual elements in the arrays. (remember than the component type of an array may itself be an array). Boaz Barak boazbk@xxxxxxxxxxxxxx (also: boazbk@xxxxxxxxxxxxxx) ----- Original Message ----- From: John Ragan <jragan@xxxxxxxxxxxxxx> To: Advanced Java <advanced-java@xxxxxxxxxxxxxxxx> Cc: <ragan_john@xxxxxxxxxxx> Sent: ùáú 02 àå÷èåáø 1999 00:55 Subject: Typecasting Array Classes from Object[] to String[] > Lets say I have the following method: > > public Object[] merge( Object[] a, Object[] b ); > > I then need to create the following method, and would like to re-use the > first in it: > > public String[] merge( String[] a, String[] b ); > > > In a simple-minded way, it would be ideal to do: > > public String[] merge( String[] a, String[] b ) > > > return ( String[] ) merge( ( Object[] )a, ( Object[] )b ); > } > > But, of course I can't, because while you can typecast Object to String, you > can't typecast Object[] to String[]. The best I could do would be to copy > every String in Object[] to a String[], and return that. > > Is there a way to achieve the effect of converting Object[] to String[]? > > > WE'RE HIRING! > > John Ragan > Senior Software Developer > webMethods, Inc. > 3877 Fairfax Ridge Road, 4th Floor > Fairfax, VA 22030 USA > Ph : 703-460-2523 > Fax: 703-460-2599 > Email: jragan@xxxxxxxxxxxxxx > > > --- > To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx > To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx > --- To unsubscribe, mail advanced-java-unsubscribe@xxxxxxxxxxxxxxxx To get help, mail advanced-java-help@xxxxxxxxxxxxxxxx
- Next by Date: interface vs. extending
- Next by thread: interface vs. extending
- Index(es):