wood burning stoves*
The moose likes Java in General and the fly likes Array of Objects to be passed into a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Array of Objects to be passed into a method" Watch "Array of Objects to be passed into a method" New topic
Author

Array of Objects to be passed into a method

rudolf hitler
Ranch Hand

Joined: Feb 07, 2003
Posts: 34
i have an array of objects i ant to pass each element of this array to a method that accepts strings as arguments.I would like to know how i could convert this array of objects to an array of strings and pass it to the method.
I casting the object to string but it gives me an error.
Phil Chuang
Ranch Hand

Joined: Feb 15, 2003
Posts: 251
Try using .toString() - it's an Object function, though if the object hasn't overriden it, I think it returns the memory address or something. Either way, it should work.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
If you are certain that the Objects are all actually Strings, you can just create a new String array and copy each reference form the Object[] to the String[]. You will probably need a for loop to do this.


Java API Documentation
The Java Tutorial
rudolf hitler
Ranch Hand

Joined: Feb 07, 2003
Posts: 34
well thanx u guys i tried both before posting the message to youll but both dont work it gives errors like u cant use toString() and i used toArray() to convert the arraylist to an array so like
Object i[]=array.toArray();
when i used String there instead of Object it again gives errors.So both of them dont work pls help its damn urgent
john guthrie
Ranch Hand

Joined: Aug 05, 2002
Posts: 124
i've never heard of anything that "gives errors like u cant use toString()" - are you saying that the following code won't work:
for (int i = 0; i < array.length; i++) {
someObject.stringMethod(array[i].toString());
}
gives you an error? if so, what error?
as for casting the whole Object array to a String array, i don't think java is able to do that. but it sounds like you don't need to anyway - if i read correctly, the method takes a single string as an arg, yes?
Leandro Oliveira
Ranch Hand

Joined: Nov 07, 2002
Posts: 298
I agree with john. As john said, this situation causes no dificulty. By calling toString() on someObject you will have the String representation of that object, every object has this method and it does return a String.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451

Originally posted by John Guthrie:
as for casting the whole Object array to a String array, i don't think java is able to do that.

If the Object array is a String array, you certainly can. Try this:


Originally posted by rudolf hitler
used toArray() to convert the arraylist to an array so like
Object i[]=array.toArray();

If all of the objects in your ArrayList are Strings then you can cast the Object array returned by toArray() to a String array like this:

If any of the objects in the list are not Strings, then the cast will fail with an ArrayStoreException.
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
john guthrie
Ranch Hand

Joined: Aug 05, 2002
Posts: 124
oops, you're right of course. i meant to say that java won't automatically "toString" an array of Objects to give you back an array of Strings.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array of Objects to be passed into a method