Hi,
can anyone tell me how can i cast Object[] to String[]
am trying this,
Object[] Value={abc,xyz}
String[] Value1=(String[]) Value1;
this not working if anyone knw how to do this plz help me.
Thanks in advance.
Printable View
Hi,
can anyone tell me how can i cast Object[] to String[]
am trying this,
Object[] Value={abc,xyz}
String[] Value1=(String[]) Value1;
this not working if anyone knw how to do this plz help me.
Thanks in advance.
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,
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);
}
}
}
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)
Here is another example, using List
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);
}
}
What jdk version did you use?
Am using jdk1.6.0_04.
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?