casting object[] to string[]??

D

djbitchpimp

I am trying to cast an array of objects to an array of strings.
Basically like this:

String [] hosts ;
hosts = new String [3] ;
hosts [0] = "host1" ;
hosts [1] = "host2" ;
hosts [2] = "host3" ;

I then pass the array to a function that takes a type Object as
paramater. Inside the function, I want to get the Object back to a
String array.

I have tried:

hosts = (String []) o ;

but all my values come up null. Is this the correct way to cast this?
 
M

Mike Schilling

I am trying to cast an array of objects to an array of strings.
Basically like this:

String [] hosts ;
hosts = new String [3] ;
hosts [0] = "host1" ;
hosts [1] = "host2" ;
hosts [2] = "host3" ;

I then pass the array to a function that takes a type Object as
paramater. Inside the function, I want to get the Object back to a
String array.

I have tried:

hosts = (String []) o ;

but all my values come up null. Is this the correct way to cast this?

Yes, and an invalid cast wouldn't empty out the array. (It would throw an
exception.) The problem is elsewhere. Can you construct a simple example
that demonstrates the problem?
 
A

A. Bolmarcich

I am trying to cast an array of objects to an array of strings.
Basically like this:

String [] hosts ;
hosts = new String [3] ;
hosts [0] = "host1" ;
hosts [1] = "host2" ;
hosts [2] = "host3" ;

I then pass the array to a function that takes a type Object as
paramater. Inside the function, I want to get the Object back to a
String array.

I have tried:

hosts = (String []) o ;

but all my values come up null. Is this the correct way to cast this?

Yes. Please provide a small complete program that demonstrates the
problem. Here is a program based on your description, but it does
not have the problem.

public class Test {
public static void main(String [] args) {
String [] hosts;
hosts = new String[3];
hosts[0] = "host1";
hosts[1] = "host2";
hosts[2] = "host3";
doit(hosts);
}

static void doit(Object o) {
String [] hosts = (String []) o;

for (int i = 0; i < hosts.length; i++) {
System.out.println("hosts[" + i + "] = " + hosts);
}
}
}
 
J

Joan

A. Bolmarcich said:
I am trying to cast an array of objects to an array of
strings.
Basically like this:

String [] hosts ;
hosts = new String [3] ;
hosts [0] = "host1" ;
hosts [1] = "host2" ;
hosts [2] = "host3" ;

I then pass the array to a function that takes a type Object
as
paramater. Inside the function, I want to get the Object back
to a
String array.

I have tried:

hosts = (String []) o ;

but all my values come up null. Is this the correct way to
cast this?

Yes. Please provide a small complete program that demonstrates
the
problem. Here is a program based on your description, but it
does
not have the problem.

public class Test {
public static void main(String [] args) {
String [] hosts;
hosts = new String[3];
hosts[0] = "host1";
hosts[1] = "host2";
hosts[2] = "host3";
doit(hosts);
}

static void doit(Object o) {
String [] hosts = (String []) o;

for (int i = 0; i < hosts.length; i++) {
System.out.println("hosts[" + i + "] = " + hosts);
}
}
}


Do you think clone() would work here ?
 
R

Roedy Green

but all my values come up null. Is this the correct way to cast this?

you can't cast an Object[] to a String[] unless it truly is a
String[]. You would do it like this:

String[] names = (String[]) objects;

An Object[] whose elements all happen to be Strings is a completely
different animal from a String[] and cannot be cast into one.
 
A

A. Bolmarcich

Yes. Please provide a small complete program that demonstrates
the
problem. Here is a program based on your description, but it
does
not have the problem.

public class Test {
public static void main(String [] args) {
String [] hosts;
hosts = new String[3];
hosts[0] = "host1";
hosts[1] = "host2";
hosts[2] = "host3";
doit(hosts);
}

static void doit(Object o) {
String [] hosts = (String []) o;

for (int i = 0; i < hosts.length; i++) {
System.out.println("hosts[" + i + "] = " + hosts);
}
}
}


Do you think clone() would work here ?


Work where?
 
M

Mike Schilling

Joan said:
A. Bolmarcich said:
I am trying to cast an array of objects to an array of strings.
Basically like this:

String [] hosts ;
hosts = new String [3] ;
hosts [0] = "host1" ;
hosts [1] = "host2" ;
hosts [2] = "host3" ;

I then pass the array to a function that takes a type Object as
paramater. Inside the function, I want to get the Object back to a
String array.

I have tried:

hosts = (String []) o ;

but all my values come up null. Is this the correct way to cast this?
Do you think clone() would work here ?

If by "work" you mean "cause the OP's probem", no.
 
M

Mike Schilling

post the exact code or better an sscce. see
http://mindprod.com/jgloss/snippet.html

That's a good explanation of the value of snippets. One point you might add
is :

Constructing the snippet will often help you determine
what the problem is and fix it yourself.

That is, when you don't understand a problem, isolating it to a simple case
is often a useful debugging step. (Particularly a problem like this one,
which doesn't involve any mysterious behavior. Winnowing it down will
probably point to the spot where the empty array is getting substituted for
the initialized one.)
 
R

Roedy Green

Constructing the snippet will often help you determine
what the problem is and fix it yourself.

I should combine the two entries for snippet and SSCCE. The SSCCE
entry makes that point.
 
T

Thomas G. Marshall

Roedy Green coughed up:
but all my values come up null. Is this the correct way to cast this?

you can't cast an Object[] to a String[] unless it truly is a
String[]. You would do it like this:

String[] names = (String[]) objects;

An Object[] whose elements all happen to be Strings is a completely
different animal from a String[] and cannot be cast into one.


Correct. To be clear to the OP however, and in case he is indeed a newbie,
take the following example:

// Object[] objs = {"hello", "there"};
Object[] objs = new String[]{"hello", "there"};

String[] strs = (String[])objs;
System.out.println(strs);

The code as presented works. This is because the objs array is
polymorphically (think "underneath") really a String[]. If however I were
to uncomment line1 above, and comment out line2 above, it /will/ compile,
but yield a /run-time/ class cast exception.

In each of those lines, there are three objects involved. The array itself,
and the 2 objects it holds. Looking at the code think of it this way:

This is /also/ broken for the same reason:

Object[] objs = {new Thingy(), new OtherThingy()};

String[] strs = (String[])objs; // >Ka-boom<
System.out.println(strs);

The objs array does not hold a String[] here either (which is made painfully
obvious).
 
T

Thomas G. Marshall

Roedy Green coughed up:
On Fri, 23 Sep 2005 14:38:51 GMT, "Thomas G. Marshall"
// Object[] objs = {"hello", "there"};

this is shorthand for:
Object[] objs = new Object[] {"hello", "there"};

Strings are objects.


(???) Sure.

I don't understand the point of this post.
 
R

Roedy Green

I don't understand the point of this post.
It was not aimed at you, but at a newbie who may have not realised the
expression on the rhs was an Object[] not a String[].
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,470
Messages
2,571,809
Members
48,797
Latest member
PeterSimpson
Top