views:

125

answers:

4

Hi, everyone,

I have a question about Java. I have an Object[] (Java default, not the user-defined) and I want to convert it to a String[]. Can anyone help me? thank you.

+6  A: 

this is conversion

for(int i = 0 ; i < objectArr.length ; i ++){  
try{
strArr[i] = objectArr[i].toString();
}catch(NullPointerException ex){
// do some default initilization
}

}  

This is casting

String [] strArr = (String[]) objectArr;  //this will give you class cast exception

Update:

Tweak 1

 String[] stringArray = Arrays.copyOf(objectArray, objectArray.length, String[].class);

Tweak2

 Arrays.asList(Object_Array).toArray(new String[Object_Array.length]);

Note:That only works if the objects are all Strings; his current code works even if they are not

forTweak1 :only on Java 1.6 and above

org.life.java
Having `Object` as an identifier seems like a bad idea, and the casting you suggests result in a `ClassCastException`.
aioobe
This doesn't work: String [] strArr = (String[]) objectArr[];
nanda
@nanda removed those []
org.life.java
Another issue: if the object array happens to contain null, you'll get an NPE with this solution. Better to go through **String.valueOf** as I do in my answer.
aioobe
@aiobee correct, updated
org.life.java
String [] strArr = (String[]) objectArr; this still won't work!
nanda
(Unless the objectArr is in fact a String-array, but I doubt it is)
aioobe
objectArr is Object[]! it is in the question...
nanda
Well sure, but an `Object[]` variable could contain a `String[]`. Try for instance `Object[] objs = new String[] { "hello", "world" };` (Though, as I said, I doubt that this is the case here.)
aioobe
@nanda @aioobe Thanks , Updated the final answer
org.life.java
aiobe: I try this: Object[] objs = new String[] { "hello", "world" }; strArr = (String[]) objectArr; Still CCE
nanda
nanda: it should be `(String[]) objs` in the end :-)
aioobe
@nanda Yes you are correct it will give you classcastexception @ runtime as we are not sure what object is holding
org.life.java
@org.life.java, but both "tweaks" will give an ArrayStoreException if you have anything but strings in the object array though.
aioobe
it should be (String[]) objs in the end :-) :-/ can you write a code where String[] strArr = (String[]) objectArr; doesn't throw any exception?
nanda
@nanda: `Object[] objs = new String[] { "hello", "world" }; String[] strArr = (String[]) objs;`
aioobe
@aioobe: you're right... sorry
nanda
+2  A: 

I think this is the simplest way if all entries in objectArr are String:

for(int i = 0 ; i < objectArr.length ; i ++){
    strArr[i] = (String) objectArr[i];
}
nanda
gives a class cast exception if you have anything but strings in the object array...
aioobe
not if you following my instructions :) : I think this is the simplest way *if all entries in objectArr are String*:
nanda
sorry. you're right. my bad.
aioobe
+3  A: 

Simply casting like this String[] strings = (String[]) objectArray; probably won't work.

Try something like this:

public static String[] asStrings(Object... objArray) {
    String[] strArray = new String[objArray.length];
    for (int i = 0; i < objArray.length; i++)
        strArray[i] = String.valueOf(objArray[i]);
    return strArray;
}

You could then use the function either like this

Object[] objs = { "hello world", -1.0, 5 };
String[] strings = asStrings(objs);

or like this

String[] strings = asStrings("hello world", -1.0, 5);
aioobe
It won't handle NPE, with an array this could be often the case.
Colin Hebert
Ops. It should be `String.valueOf` (had it in an earlier revision :-) corrected. Thanks.
aioobe
+2  A: 

I guess you could also use System.arraycopy

System.arraycopy(objarray, 0, strarray, 0, objarray.length);

provided, strarray is of the length objarray.length and objarray contain only strings. Or it would throw ArrayStoreException. See aioobe's comment.

bdhar
...and provided objarray contains only strings... otherwise you'll run into an ArrayStoreException.
aioobe
Thanks aioobe! I was about to edit my post.
bdhar