At 07:30 AM 1/31/2001 -0500, Thom Burnett wrote:
>John,
>
>I've seen the problem before and learned just do the interation and casts
>without toArray().
>
>My listing of the Java API on Vector lists only two methods toArray()
>One that takes no parameters and one that takes an Object[]. The text with
>the latter says that it sorts the array (with no information about how it
>sorts, ascending?)
It says "Returns an array containing all of the elements in this
Vector in the correct order". By "correct order", they mean in the
same order as was in the Vector. So
theArray[i] == theVector.elementAt(i)
for 0 <= i < theVector.size(). Likewise for the ArrayList versions
of the toArray() methods.
One tiny pet peeve of mine is that you can't use toArray(Object[])
to initialize a null array variable. That is:
String[] stringArray = null;
ArrayList stringList = buildStringList();
stringArray =
(String[]) stringList.toArray(stringArray); // <- throws NPE!
The reason is that toArray() gets a null variable, and has no clue
what it is supposed to be an array of. You have to do it like this:
stringArray = (String[]) stringList.toArray (new String[0]);
which is kinda inelegant, because you have to create a new object.
(I'll sometimes keep a static empty String array around for this
purpose.) Another way is to bite the bullet and stick in an extra
line of code:
stringArray = new String[stringList.size()];
stringArray = (String[]) stringList.toArray (stringArray);
>There's nothing about toArray(Class?) or any other overloaded operators.
>Is the API faulty? Am I looking at the wrong API ?
>Is this only Java 1.3? Somewhere else?
I wouldn't mind something like this:
stringArray = (String[]) stringList.toArray (String.class);
SFAIK, it wouldn't break any rules to have such a thing.
_______________________________________________
Swing mailing list
[EMAIL PROTECTED]
http://eos.dk/mailman/listinfo/swing