• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array of Objects to be passed into a method

 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 251
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
rudolf hitler
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 298
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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
 
john guthrie
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Well THAT's new! Comfort me, reliable tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic