3/11/2008

How to initialize a list when declaring it

With array, we can easily declare and initialize it at the same time:

String[] favorites = new String[] {"EJB", "JPA", "GlassFish"};
Or even simpler:
String[] favorites = {"EJB", "JPA", "GlassFish"};
We can do the same with a List using java.util.Arrays.asList method. For example:
package javahowto;

import java.util.Arrays;
import java.util.List;

public class ListTest {
public static final List<String> favorites =
Arrays.asList("EJB", "JPA", "GlassFish");

public static void main(String[] args){
System.out.println("favorites: " + favorites);
}
}

13 comments:

Robert J Miller said...

thanks!

CrAzYCakes said...

Why thanks!was looking for this

Jagat said...

Arrays.asList returns an immutable list. So you can't add any element to the list later.

Karola said...

Thanks, just what I needed to check against a list of constants.

Reece said...

Thank you, this is quite a good little gem I wasn't aware of!

Brian said...

[quote=Jagat]
Arrays.asList returns an immutable list. So you can't add any element to the list later.
[/quote]

While this is true, you can also pass the result of Arrays.asList() to the ArrayList constructor (or any other classes constructor that implements List). For example:

[code]
public static final List<String> favorites = new ArrayList<String>(Arrays.asList("EJB", "JPA", "Glassfish", "NetBeans"));

[/code]

Justin said...

Thanks for explaining it very well

Anonymous said...

Arrays.asList()

Not

Array.asList()

Daniel said...

Thank you!

Anna said...

Great and Useful Article.

Online Java Course

Java Online Training

Java Course Online

J2EE training

online J2EE training

Best Recommended books for Spring framework

Java Interview Questions












Java Training Institutes in Chennai

Java Training in Chennai

J2EE Training in Chennai

java j2ee training institutes in chennai

Vũ Diệu Linh said...

I enjoyed on reading your blog post. Very interesting to read this article.I would like to thank you for the efforts you had made for writing this awesome article. Please visit my website, Friv 4000 Games is where all the free friv games.
Friv 4000

eMexo Technologies said...

Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

Dot net training in electronic city

Web designing & Development course in Chennai said...

I like the helpful info you provide in your articles. I’ll bookmark your weblog and check again here regularly. I am quite sure I will learn much new stuff right here! Good luck for the next!
Web Designing Training Institute in Chennai | web design training class in chennai | web designing course in chennai with placement | Web Designing and Development Course in Chennai | Web Designer Training Course in Chennai
Mobile Application Development Courses in chennai
Data Science Training in Chennai | Data Science courses in Chennai
Professional packers and movers in chennai | PDY Packers | Household Goods Shifting
Web Designing Training Institute in Chennai | Web Designing courses in Chennai
Google ads services | Google Ads Management agency
Web Designing Course in Chennai | Web Designing Training in Chennai