hi all,
how do i cast an array of type Object to a array of type String. see code
below.
public class test() {
public static void main(String args[]) {
String[] s = null;
Object[] o = {"Chua", "Soon", "Dee"};
s = (String [])o;
for (int i=0;i<s.length;i++) {
System.out.println(s[i]);
}
}
}
the above code will issue a ClassCastException. how do i go about it! thanks
09-13-2001, 10:50 AM
Paul Clapham
Re: array cast
You cast each of the elements individually (in a loop).
PC2
"cloud" <soondee.chua@sesami.com> wrote in message
news:3ba0586a@news.devx.com...
> hi all,
> how do i cast an array of type Object to a array of type String. see
code
> below.
>
> public class test() {
> public static void main(String args[]) {
> String[] s = null;
> Object[] o = {"Chua", "Soon", "Dee"};
> s = (String [])o;
> for (int i=0;i<s.length;i++) {
> System.out.println(s[i]);
> }
> }
> }
>
> the above code will issue a ClassCastException. how do i go about it!
thanks
>
>
09-13-2001, 09:04 PM
cloud
Re: array cast
hi,
is there a more efficient way to do this. thanks.
cloud
"Paul Clapham" <pclapham@core-mark.com> wrote in message
news:3ba0c6e4@news.devx.com...
> You cast each of the elements individually (in a loop).
>
> PC2
>
> "cloud" <soondee.chua@sesami.com> wrote in message
> news:3ba0586a@news.devx.com...
> > hi all,
> > how do i cast an array of type Object to a array of type String. see
> code
> > below.
> >
> > public class test() {
> > public static void main(String args[]) {
> > String[] s = null;
> > Object[] o = {"Chua", "Soon", "Dee"};
> > s = (String [])o;
> > for (int i=0;i<s.length;i++) {
> > System.out.println(s[i]);
> > }
> > }
> > }
> >
> > the above code will issue a ClassCastException. how do i go about it!
> thanks
> >
> >
>
>
09-14-2001, 10:13 PM
ljubisak
Re: array cast
No, there is not more efficient way of casting array then one you've done
first time.
Your problem is not related to the Array but to the general rule for object
reference casting at runtime.
The new type must be a same class or must be some superclass of the old type
class. In other words casting String to Object is legal at runtime and the
way how you did it is illegal.
(Or-String has and does everything what Object does (String is the Object),
but Object doesn't have String capabilities (Object is not the String))
"cloud" <soondee.chua@sesami.com> wrote:
>hi,
> is there a more efficient way to do this. thanks.
>
>cloud
>
>"Paul Clapham" <pclapham@core-mark.com> wrote in message
>news:3ba0c6e4@news.devx.com...
>> You cast each of the elements individually (in a loop).
>>
>> PC2
>>
>> "cloud" <soondee.chua@sesami.com> wrote in message
>> news:3ba0586a@news.devx.com...
>> > hi all,
>> > how do i cast an array of type Object to a array of type String. see
>> code
>> > below.
>> >
>> > public class test() {
>> > public static void main(String args[]) {
>> > String[] s = null;
>> > Object[] o = {"Chua", "Soon", "Dee"};
>> > s = (String [])o;
>> > for (int i=0;i<s.length;i++) {
>> > System.out.println(s[i]);
>> > }
>> > }
>> > }
>> >
>> > the above code will issue a ClassCastException. how do i go about it!
>> thanks
>> >
>> >
>>
>>
>
>
09-16-2001, 05:36 AM
Ran Kornfeld
Re: array cast
You can create a new String type array and then use the arraycopy method.
Code Example:
...
private Object[] arr = {"one","two","three"};
public void convertAndType() {
String[] strArr=new String[arr.length];
System.arraycopy(arr,0,strArr,0,arr.length);
for (int i=0;i<arr.length;i++) {
System.out.println(strArr[i] +" "+strArr.getClass().getName());
}
}
"cloud" <soondee.chua@sesami.com> wrote:
>hi all,
> how do i cast an array of type Object to a array of type String. see code
>below.
>
>public class test() {
> public static void main(String args[]) {
> String[] s = null;
> Object[] o = {"Chua", "Soon", "Dee"};
> s = (String [])o;
> for (int i=0;i<s.length;i++) {
> System.out.println(s[i]);
> }
> }
> }
>
>the above code will issue a ClassCastException. how do i go about it! thanks
>
>
09-16-2001, 08:25 PM
ljubisak
Re: array cast
I already responded you that you can't cast Object to String (if that Object
wasn't created as the String) and to make your code working, maybe the simpliest
way is to get rid of the second Array.
public class test() {
public static void main(String args[]) {
Object[] o = {"Chua", "Soon", "Dee"};
for (int i=0;i<o.length;i++) {
System.out.println(o[i]);
}
}
}
"cloud" <soondee.chua@sesami.com> wrote:
>hi all,
> how do i cast an array of type Object to a array of type String. see code
>below.
>
>public class test() {
> public static void main(String args[]) {
> String[] s = null;
> Object[] o = {"Chua", "Soon", "Dee"};
> s = (String [])o;
> for (int i=0;i<s.length;i++) {
> System.out.println(s[i]);
> }
> }
> }
>
>the above code will issue a ClassCastException. how do i go about it! thanks
>
>
08-03-2002, 01:09 PM
Peter
RE: Re: array cast
/*May be the printing is not the issue but acquiring the String array.
There is one (perhaps more efficient) method when the Automatic cast of element types takes the place and this is System.arraycopy. E.g. following variant works:
*/
public class test {
public static void main(String args[]) {
const int FIRST = 0;
Object[] obj = {"Chua", "Soon", "Dee"};
String[] str = new String[obj.length];
System.arraycopy(obj,FIRST,str,FIRST,obj.length);
for (int i=0;i<s.length;i++) {
System.out.println(s[i]);
}
}
}
/*
I had the same problem with oracle.sql.ARRAY's getOracleArray() method.
It returns oracle.sql.Datum[] and I needed oracle.sql.CHAR[]
above method worked as well
*/
>I already responded you that you can't cast Object to String (if that Object
>wasn't created as the String) and to make your code working, maybe the simpliest
>way is to get rid of the second Array.
>
>
>public class test() {
> public static void main(String args[]) {
> Object[] o = {"Chua", "Soon", "Dee"};
> for (int i=0;i<o.length;i++) {
> System.out.println(o[i]);
> }
> }
> }
>
>
>
>
>"cloud" <soondee.chua@sesami.com> wrote:
>>hi all,
>> how do i cast an array of type Object to a array of type String. see code
>>below.
>>
>>public class test() {
>> public static void main(String args[]) {
>> String[] s = null;
>> Object[] o = {"Chua", "Soon", "Dee"};
>> s = (String [])o;
>> for (int i=0;i<s.length;i++) {
>> System.out.println(s[i]);
>> }
>> }
>> }
>>
>>the above code will issue a ClassCastException. how do i go about it! thanks
>>
>>
>