Results 1 to 7 of 7
Thread: Casting problem
- 05-14-2008, 10:38 AM #1
Member
- Join Date
- May 2008
- Posts
- 8
- Rep Power
- 0
- 05-14-2008, 10:58 AM #2
I don't know if that is allowed now in the latest jdk release...
My friend sent a request about that feature a few months ago.
As far as i know, you cannot just do that. You have to loop over their elements and do the casting.
here is an example,
Java Code:public class test{ public static void main(String args[]){ Object[] t = {"I","am","who","am"}; String[] s = new String[t.length]; int x=0; for(Object temp:t){ s[x] = (String)temp;x++; } for(String temp:s){ System.out.println(temp); } } }freedom exists in the world of ideas
- 05-14-2008, 11:09 AM #3
Member
- Join Date
- May 2008
- Posts
- 8
- Rep Power
- 0
HI sukatoa,
I tried in ur way but following error occured
The java class could not be loaded. java.lang.UnsupportedClassVersionError: (Unsupported major.minor version 49.0)
- 05-14-2008, 11:16 AM #4
Here is another example, using List
Java Code:import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; public class test{ static ArrayList<Object> a = new ArrayList<Object>(); static ArrayList<String> al; public static void main(String args[]){ Object[] t = {"I","am","a","Java","Programmer"}; a.addAll((Collection)Arrays.asList(t)); al = new ArrayList<String>((Collection)a); for(String castedInternally:al) System.out.println(castedInternally); } }freedom exists in the world of ideas
- 05-14-2008, 11:17 AM #5
What jdk version did you use?
freedom exists in the world of ideas
- 05-14-2008, 11:24 AM #6
Member
- Join Date
- May 2008
- Posts
- 8
- Rep Power
- 0
Am using jdk1.6.0_04.
- 05-14-2008, 11:29 AM #7
Im using update 3....
There should be a problem at your side....
those are compilable and runnable code....
But, my example has a different approach,
all objects inside the Array of object is a String representation.
How about that {abc,xyz}?
Are abc and xyz are Strings too? or has a String representation?Last edited by sukatoa; 05-14-2008 at 01:56 PM.
freedom exists in the world of ideas
Similar Threads
-
Casting an int value into a char
By kurtulas in forum New To JavaReplies: 2Last Post: 02-16-2008, 08:03 PM -
Type Casting Help
By rhm54 in forum New To JavaReplies: 2Last Post: 02-07-2008, 12:06 PM -
'Casting' couch !!!!
By ajaygargnsit in forum Advanced JavaReplies: 4Last Post: 01-04-2008, 04:54 PM -
'Casting' couch !!
By ajaygargnsit in forum New To JavaReplies: 1Last Post: 12-22-2007, 01:05 PM -
Casting
By leebee in forum New To JavaReplies: 5Last Post: 08-10-2007, 12:24 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks