Ross said:
(JButton[])(map.values().toArray(new
JButton[map.values().size()]));
It's a common misconception that this method puts the
values in the array you give it, but this isn't true -
instead it creates a new array of the same type as the
passed array.
"If the collection fits in the specified array, it is
returned therein. Otherwise, a new array is allocated with
the runtime type of the specified array and the size of this
collection." (java.util.Collection javadoc for toArray with
a parameter).
This program prints "true":
import java.util.List;
import java.util.Arrays;
public class TestToArray {
public static void main(String[] args) {
String[] data = {"a","b","c"};
List l = (List) Arrays.asList(data);
String[] myArray = new String[l.size()];
String[] listArray = (String[])(l.toArray(myArray));
System.out.println(myArray==listArray);
}
}
I have never had to use one of these is a sufficiently
performance critical environment to care whether presizing
is faster or slower than using a zero-length array.
Patricia