Monday, November 20, 2006

One less headache thanks to Tiger

You are familiar with the old Arrays.asList(Object[]) call for converting an array to a List. You're also sick of doing this:

List list = new ArrayList();
list.add(Integer.valueOf(1));
list.add(Integer.valueOf(2));
list.add(Integer.valueOf(3));

Well, thanks to autoboxing and varargs, you can do this instead:

List list = Arrays.asList(1, 2, 3);

Sure, it's not a huge deal, but it's just better in every sense of the word.

3 comments:

Anonymous said...

list = [1, 2, 3]

toby451 said...

One should know that the resulting ArrayList is kind of weak. For instance ... íf you like to remove from it you'll get a UnsupportedOperationException. Another wrap solves that issue (i.e. new ArrayList(Arrays.asList ... ) but now thing does not look quite as nice anymore. :(

/Tobias

toby451 said...

(but that has nothing to do with Tiger of course)