TCLUG Development Archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [TCLUG-DEVEL:267] Vector.toArray()




--- Mark Phillips <mbp@geomtech.com> wrote:
> 
> > Am I missing something here?
> > 
> > I want to convert a Vector of Strings to an array of Strings.
> > 
> > Vector foo = new Vector()
> > foo.add("1");
> > foo.add("2");
> > foo.add("3");
> > foo.add("4");
> > 
> > String[] blah = new String[4];
> > blah = (String[])foo.toArray();
> > 
> > Should this work?
> 
> I just did some experiments and it looks like you need to either
> 
>     (1) pass 'blah' as an argument to toArray, as in
> 
> 	   blah = (String[])foo.toArray(blah);
> 


If you do it this way, and you know that you are passing in an array
that is large enough to hold all the elements in the Container, you do
not need to assign the return value.  It will fill the argument with
the elements.  If there is not enough room, it will create a new array
of the same type.  This is something to be carefull of if you have
values you need in the array you are thinking about passing in.

__________________________________________________
Do You Yahoo!?
Send instant messages & get email alerts with Yahoo! Messenger.
http://im.yahoo.com/