Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Cast Object[] to String[]

Status
Not open for further replies.
May 13, 2002
75
0
0
GB
Is there an easy way to convert (cast) a Object array to a String array ?

thanks Alistair [monkey]
 
You can't cast the array, so you'll have do something like..

String[] strArray = new String[array.length];
for(int i=0;i<array.length;i++) strArray = (String)array;

where array is an Object[] where each element is a String.
 
You could do something like this and write a toString method for your object. Then you would get a string representation of your object

class MyObject{

private String name;

public MyObject(String aName){
name=aName;
}//constructor

//toString method...
public String toString(){
return new String(name);
}
}//class

Then in your code you can just call the toString method like this...

MyObject anObject;

Object[] objArray={new MyObject(&quot;1&quot;),new MyObject(&quot;2&quot;)};
String[] strArray = new String[objArray.length];

for(int i=0;i<strArray.length;i++){
//call the toString method you write for your object
strArray = objArray.toString();
System.out.println(&quot;&quot;+strArray);
}

There is a toString method in the class Object, which brings back the name of the class and it's memory location as a string eg MyObject@253498

 
hmmmm seems to be taking a few characters away on posting, The square brackets that refer to the array location clashing with markup. whoops!
Supposed to read like this...
Code:
for(int i=0;i<strArray.length;i++){
      strArray[i] = objArray[i].toString();
      System.out.println(&quot;&quot;+strArray[i]);
}
sorry!!
 
Hmm likewise with my initial response..

it should read..

String[] strArray = new String[array.length];
for(int n=0;n<array.length;n++) strArray[n] = (String)array[n];

where array is an Object[] where each element is a String.

NOTE: in future dont type [letter-i] as it will be translated as italic.




 
You can cast an Object array to a String array. Example code:
Code:
public class Test {

  private static String[] s = { &quot;1&quot;, &quot;2&quot;, &quot;3&quot; };

  public static Object method1() {
    return s;
  }

  public static Object[] method2() {
    return s;
  }

  public static void main(String[] args) {

    String[] s2;
    s2 = (String[]) method1();
    for(int i = 0; i < s2.length; i++)
      System.out.println(i+&quot;: &quot;+s2[i]);
    s2 = (String[]) method2();
    for(int i = 0; i < s2.length; i++)
      System.out.println(i+&quot;: &quot;+s2[i]);
  }
}
Cheers, Neil :)
 
But, the question was catsing an Object[] to a String[], your solution surely doesn't work the other way round.
 
To make it clear, Neil, your code doesn't work if the static s is an Object[].
 
If the Object array IS a String array, yes, you can cast it. If was instantiated as an Object array or an array of some type other than String then, no, you cannot cast it.
 
Actually, the code does work if static s is an Object[], namely:
Code:
  private static Object[] s = new String[] { &quot;1&quot;, &quot;2&quot;, &quot;3&quot; };
But as meadandale states, this won't work if the Object[] reference does not point to a String[] on the heap. For a truly random array of Objects, you would need to loop through, and call toString on each element in turn. I was just being pedantic in that YES, you can cast an Object[] reference to a String[] reference, so long as the object on the heap is a String[] object. ;-)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top