How Java initializes ArrayList with a single line of code

Source: Internet
Author: User

This article is from: Http://stackoverflow.com/questions/1005073/initialization-of-an-arraylist-in-one-line Note: The Chinese content of this article may be machine translation, To view the original English, click on the link above.

I would like to create a list of options to test something.

I am doing:

  arraylist<string> places = new  arraylist<string > ();  
Places. "Buenos Aires" Places. Add Places. Add "La Plata" /span>

I refactor the code to do:

ArrayList<String> places = new ArrayList<String>(Arrays.asList("Buenos Aires", "Córdoba", "La Plata"));

Is there a better way to do that?

Thanks for reading!

Workaround 1:

In fact, you might want to initialize the "best" way, ArrayList is the way you write, because it doesn't need to create a new List in any way:

 arraylist<string>  list = newarraylist<string> ();      List. Add "A"      List. Add "B"      List. Add "C"                  /span>          

Catch is quite a lot of typing required, see list instances.

A method that resembles an instance initializer (also known as a "double curly brace Initialization") for an anonymous inner class:

  arraylist<string> list = new  arraylist<string > ()  {{ 
    Add ( "A" );
    Add ( "B"     Add ( "C" }}
/span>

However, I don't really like the method because what you end up being a subclass of a class ArrayList has an instance initializer and this class just to create an object--just looks like I'm a bit overqualified.

What's good is if you accept the item coin Collection text Suggestions, we can have the text in the list in Java 7:

List<String> list = ["A", "B", "C"];

Unfortunately it will not help you here, as it will initialize one List , and not ArrayList , moreover, it has not yet been provided if it ever will and.

Workaround 2:

Would it be easier if you just declared it to be ArrayList in the list?

List<String> places = Arrays.asList("Buenos Aires", "Córdoba", "La Plata");
Workaround 3:
ImportCOM.  Google.  Common.  Collect.  Immutablelist;

....

List<String> places = immutablelist. of ("Buenos Aires", "Córdoba", "La Plata");
Workaround 4:

Pretty much, the simplest way is List<String> strings = new ArrayList<String>(asList("foo", "bar", "baz")) if you have to have one ArrayList , otherwise you should just use it in most casesList<String> strings = asList("foo", "bar", "baz")

You say you have announced as a list one ArrayList in your code, but you should not do it unless you are using some members ArrayList in this is not List , for example ArrayList.ensureCapacity() .

Usually you should just declare variables that you want to use, and its list is usually the most common interface List , and initialize them in concrete implementations, for example ArrayList or LinkedList .

Use is not implemented, otherwise you will find an interface that you want to change in multiple locations when you want to use another implementation.

Another example will always declare variables, one InputStream even if it is usually one FileInputStream , which is because one day soon you or someone else will want to use some alternative InputStream .

Workaround 5:

You can create a factory method:

Public Static ArrayList<String>Createarraylist(String ...Elements) {
ArrayList<String>List= New ArrayList<String> ();
For (StringElement:Elements) {
List.Addelement   }
  return List
}

....

arraylist<string > places = createarraylist (
  "S?o Paulo" "Rio de Janeiro" , Brasília ");
/span>

But it's no more than the first time you refactor.

Update: greater flexibility, it can be a common:

Public Static <T> ArrayList<T>Createarraylist(T...Elements) { 
ArrayList<T>List= new arraylist<t> ();  
  for (t element : Elements) {
    List. Addelement   }
  return List
}
/span>
Workaround 6:

Just notice it in a very simple working method as follows:

ArrayListArrlist= New ArrayList() {"1",2,3,"4" }; 

list<customer > Listcustomer = new list<customer> () { new Customer (), new Customer (), new Customer () }; /span>

This C # 3.0 does not support the work required by the two. Hope this helps.

Workaround 7:

To set a copy of the default object for the list to populate N:

ArrayList<Object> list = new ArrayList<Object>(
    Collections.nCopies(1000, new Object()));

How Java initializes ArrayList with a single line of code

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.