File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes How to dinamically populate a String array? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "How to dinamically populate a String array?" Watch "How to dinamically populate a String array?" New topic
Author

How to dinamically populate a String array?

miguel lisboa
Ranch Hand

Joined: Feb 08, 2004
Posts: 1281
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?


java amateur
Efrain Morales
Greenhorn

Joined: Oct 14, 2009
Posts: 12
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

Joined: Feb 08, 2004
Posts: 1281
that worked nicely

thanks a lot!
Siva Masilamani
Ranch Hand

Joined: Sep 19, 2008
Posts: 385

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

E.g.



SCJP 6,SCWCD 5,SCBCD 5

Failure is not an option.
John de Michele
Rancher

Joined: Mar 09, 2009
Posts: 600
Or, just use an ArrayList<String>.

John.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19543
    
  16

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to dinamically populate a String array?
 
Similar Threads
sending mail to multiple recipients
Java mail api
JavaMail sending problem
MultiDimenaional Array sort
Mail API Problem