mercredi 9 avril 2014

Java - ArrayList d'initialisation équivalente à l'initialisation du tableau - Stack Overflow


I am aware that you can initialize an array during instantiation as follows:


String[] names = new String[] {"Ryan", "Julie", "Bob"};

Is there a way to do the same thing with an ArrayList? Or must I add the contents individually with array.add()?




Arrays.asList can help here:


new ArrayList<Integer>(Arrays.asList(1,2,3,5,8,13,21));



Yes.


new ArrayList<String>(){{
add("A");
add("B");
}}

What this is actually doing is creating a class derived from ArrayList<String> (the outer set of braces do this) and then declare a static initialiser (the inner set of braces). This is actually an inner class of the containing class, and so it'll have an implicit this pointer. Not a problem unless you want to serialise it.


I understand that Java 7 will provide additional language constructs to do precisely what you want.




Here is the closest you can get:


ArrayList<String> list = new ArrayList(Arrays.asList("Ryan", "Julie", "Bob"));

You can go even simpler with:


List<String> list = Arrays.asList("Ryan", "Julie", "Bob")

Looking at the source for Arrays.asList, it constructs an ArrayList, but by default is cast to List. So you could do this (but not reliably for new JDKs):


ArrayList<String> list = (ArrayList<String>)Arrays.asList("Ryan", "Julie", "Bob")



Arrays.asList("Ryan", "Julie", "Bob");




Well, in Java there's no literal syntax for lists, so you have to do .add().


If you have a lot of elements, it's a bit verbose, but you could either:



  1. use Groovy or something like that

  2. use Arrays.asList(array)


2 would look something like:


String[] elements = new String[] {"Ryan", "Julie", "Bob"};
List list = new ArrayList(Arrays.asList(elements));

This results in some unnecessary object creation though.




This is how it is done using the fluent interface of the op4j Java library (1.1. was released Dec '10) :-


List<String> names = Op.onListFor("Ryan", "Julie", "Bob").get();

It's a very cool library that saves you a tonne of time.



I am aware that you can initialize an array during instantiation as follows:


String[] names = new String[] {"Ryan", "Julie", "Bob"};

Is there a way to do the same thing with an ArrayList? Or must I add the contents individually with array.add()?



Arrays.asList can help here:


new ArrayList<Integer>(Arrays.asList(1,2,3,5,8,13,21));


Yes.


new ArrayList<String>(){{
add("A");
add("B");
}}

What this is actually doing is creating a class derived from ArrayList<String> (the outer set of braces do this) and then declare a static initialiser (the inner set of braces). This is actually an inner class of the containing class, and so it'll have an implicit this pointer. Not a problem unless you want to serialise it.


I understand that Java 7 will provide additional language constructs to do precisely what you want.



Here is the closest you can get:


ArrayList<String> list = new ArrayList(Arrays.asList("Ryan", "Julie", "Bob"));

You can go even simpler with:


List<String> list = Arrays.asList("Ryan", "Julie", "Bob")

Looking at the source for Arrays.asList, it constructs an ArrayList, but by default is cast to List. So you could do this (but not reliably for new JDKs):


ArrayList<String> list = (ArrayList<String>)Arrays.asList("Ryan", "Julie", "Bob")


Arrays.asList("Ryan", "Julie", "Bob");



Well, in Java there's no literal syntax for lists, so you have to do .add().


If you have a lot of elements, it's a bit verbose, but you could either:



  1. use Groovy or something like that

  2. use Arrays.asList(array)


2 would look something like:


String[] elements = new String[] {"Ryan", "Julie", "Bob"};
List list = new ArrayList(Arrays.asList(elements));

This results in some unnecessary object creation though.



This is how it is done using the fluent interface of the op4j Java library (1.1. was released Dec '10) :-


List<String> names = Op.onListFor("Ryan", "Julie", "Bob").get();

It's a very cool library that saves you a tonne of time.


0 commentaires:

Enregistrer un commentaire