array cast


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 7 of 7

Thread: array cast

  1. #1
    cloud Guest

    array cast

    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



  2. #2
    Paul Clapham Guest

    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
    >
    >




  3. #3
    cloud Guest

    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
    > >
    > >

    >
    >




  4. #4
    ljubisak Guest

    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
    >> >
    >> >

    >>
    >>

    >
    >



  5. #5
    Ran Kornfeld Guest

    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
    >
    >



  6. #6
    ljubisak Guest

    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
    >
    >



  7. #7
    Peter Guest

    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
    >>
    >>

    >


    Message sent from http://www.aewnet.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center