Re: Arrays.asList() doesn't work quite like I would think it should
On 3 Oct 2007 04:30:51 GMT, ram@zedat.fu-berlin.de (Stefan Ram) wrote,
quoted or indirectly quoted someone who said :
?Changes to the returned list "write through" to the array.?
That was a surprise. I looked up Arrays.asList and there it was.
It also says .
This method also provides a convenient way to create a fixed-size list
initialized to contain several elements.
This means insert and delete would be suppressed. Shuffle just might
try to use those methods.
I got caught because I always use these idioms:
ArrayList <=> array
// NEW STYLE with generics
// converting an array to an ArrayList, with Generics
String[] animals = { "bear", "cougar", "wolverine"};
ArrayList<String> al = new ArrayList<String>( Arrays.asList( animals )
);
// converting an ArrayList to an array with generics.
String[] predators = al.toArray( new String[ al.size() ] );
// OLD STYLE, without generics
// converting an array to an ArrayList, without Generics
String[] animals = { "bear", "cougar", "wolverine"};
ArrayList al = new ArrayList( Arrays.asList( animals ) );
// converting an ArrayList to an array without generics,
String[] predators = (String[])al.toArray( new String[ al.size() ] );
--
Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com