Conversion and relationship between ArrayList and array, and arraylist array Conversion

Source: Internet
Author: User

Conversion and relationship between ArrayList and array, and arraylist array Conversion

 

The main differences between arrays and ArrayList are efficiency, type recognition, and primitive type.
Array ([]): the most efficient, but its capacity is fixed and cannot be changed dynamically;
ArrayList: The capacity can be dynamically increased, but the efficiency is sacrificed;
Suggestion:
The array is used first. It cannot be determined that ArrayList is used only when the array size is small!


Efficiency:
Array resizing is a factor that significantly affects ArrayList efficiency.
Each time you execute Add, AddRange, Insert, InsertRange, and other methods to Add elements, check whether the internal array capacity is insufficient. If yes, it will re-build an array with twice the current capacity, Copy the old elements to the new array, and then discard the old array. At this critical point, the expansion operation, it may affect efficiency.

 

List -----> Array
In development, the conversion between List and array types is inevitable. For example:
Package test. test1;
Import java. util. ArrayList;
Import java. util. List;
Public class Test {

/**
* @ Param args
*/
Public static void main (String [] args ){
List list = new ArrayList ();
List. add ("Wang lihu ");
List. add ("Zhang San ");
List. add ("Li Si ");
Int size = list. size ();
String [] array = new String [size];
For (int I = 0; I <list. size (); I ++ ){
Array [I] = (String) list. get (I );
}
For (int I = 0; I <array. length; I ++ ){
System. out. println (array [I]);
}
}
}

As mentioned above, to convert data of the ArrayList type to String [], you must traverse the List type. In fact, this is not necessary, list provides us with a good way to solve the problem of converting a List into an array. Let's look at another example:
Package test. test1;

Import java. util. ArrayList;
Import java. util. List;

Public class Test {
Public static void main (String [] args ){
List <String> list = new ArrayList <String> ();
List. add ("Wang lihu ");
List. add ("Zhang San ");
List. add ("Li Si ");
Int size = list. size ();
String [] array = (String []) list. toArray (new String [size]);
For (int I = 0; I <array. length; I ++ ){
System. out. println (array [I]);
}
}
}
Do you find that this is what you want? In fact, ArrayList provides the public <T> T [] toArray (T [] a) method to return an array containing all the elements in the list in the correct order; the runtime type of the returned array is the runtime type of the specified array. If the list can be placed in the specified array, an array containing the list elements is returned. Otherwise, a new array is allocated based on the running type of the specified array and the size of the list.
If the specified array can accommodate the list and has free space (that is, the array has more elements than the list), the elements in the array that closely follow the end of the set are set to null. This is useful for determining the length of a list, but only when the caller knows that the list does not contain any null element.

Array ---> List
So how can we convert an array into a List? Let's look at a small example, as shown below:
Package test. test1;
Import java. util. ArrayList;
Import java. util. List;
Public class Test {
Public static void main (String [] args ){
String [] array = new String [3];
Array [0] = "Wang lihu ";
Array [1] = "James ";
Array [2] = "Li Si ";
List <String> list = new ArrayList <String> ();
For (int I = 0; I <array. length; I ++ ){
List. add (array [I]);
}
For (int I = 0; I <list. size (); I ++ ){
System. out. println (list. get (I ));
}
}
}
Don't you find it very troublesome? In fact, the Arrays object for Array conversion becomes a List problem is also provided to public static <T> List <T> asList (T... a) for us to call. The following example is used for trial run:
Package test. test1;

Import java. util. Arrays;
Import java. util. List;

Public class Test {
Public static void main (String [] args ){
String [] array = new String [3];
Array [0] = "Wang lihu ";
Array [1] = "James ";
Array [2] = "Li Si ";
List <String> list = Arrays. asList (array );
For (int I = 0; I <list. size (); I ++ ){
System. out. println (list. get (I ));
}


}
}
Simply put, the asList method returns a fixed-size list supported by the specified array. This method works with Collection. toArray and serves as a bridge between array-based APIs and collection-based APIs. The returned list is serializable and implements RandomAccess. In addition, this method also provides a convenient way to create a fixed-length list, which is initialized to contain multiple elements:
Package test. test1;
Import java. util. Arrays;
Import java. util. List;
Public class Test1 {
Public static void main (String [] args ){
List <String> list = Arrays. asList ("Wang lihu", "Zhang San", "Li Si ");
For (int I = 0; I <list. size (); I ++ ){
System. out. println (list. get (I ));
}
}
}

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.