• 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

How to dinamically populate a String array?

 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i can query this pupils table to obtain an ArrayList with their email adresses
from javamail api the method to send to more then one adress at the same time is:


how can i dinamically populate the para[] String array?
 
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think you could simply use something like String [] para = myArrayList.toArray(); , as long as your ArrayList is declared as <String> type.

Doing it without that <String> type could also work, but I don't think it's safe to do it.

You could also do it manually by using

 
miguel lisboa
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that worked nicely

thanks a lot!
 
Ranch Hand
Posts: 385
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I would recommend to use instanceof operator before you cast array list element to String.

E.g.

 
Rancher
Posts: 600
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or, just use an ArrayList<String>.

John.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Efrain Morales wrote:I think you could simply use something like String [] para = myArrayList.toArray(); , as long as your ArrayList is declared as <String> type.


Not quite; the array returned by ArrayList.toArray() is always an Object[]. When you try to cast that to String[] you will get a ClassCastException. The following will work though:
As the Javadoc of Collection.toArray(T[]) says, it will make sure the runtime type of the returned array is the same as the type of the array argument.
 
This tiny ad will self destruct in five seconds.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic