Conversion between array and list of Android development

Source: Internet
Author: User

The 1.List transform becomes an array. (The list here is the entity is ArrayList)
Call the ToArray method of ArrayList.

  ToArray

Public <T> t[] ToArray (t[] a) returns an array that contains all the elements in this list in the correct order; The run-time type of the returned array is the run-time type of the specified array. If the list can fit into the specified array, the array that is placed in this list element is returned. Otherwise, a new array is allocated based on the run-time type of the specified array and the size of the list.

If the specified array can hold the list and has the remaining space (that is, the array has more elements than the list), the element immediately following the collection at the end of the array is set to NULL. This is useful for determining the length of a list, but only if the caller knows that the list does not contain any null elements.


Designated by:

ToArray in the interface collection<e>

Designated by:
ToArray in the interface list<e>

Covered:
ToArray in the class abstractcollection<e>

Parameters:
A-to store an array of list elements if it is large enough; otherwise, it is a new array with the same run-time type assigned to store the list elements.
Return:
An array that contains the list elements.
Thrown:
Arraystoreexception-If the run-time type of a is not a super-type of the run-time type for each element in this list.
Specific usage:


List List = new ArrayList ();
List.add ("1");
List.add ("2");
final int size = List.size ();
String[] arr = (string[]) List.toarray (new string[size]);


2. The array is converted into a list. Call the Aslist method of arrays.
Aslist

public static <T> list<t> aslist (T ... a) returns a list of fixed sizes supported by the specified array. (Changes to the returned list are "written straight" to the array.) This approach, together with Collection.toarray, serves as a bridge between an array-based API and an Collection-based API. The returned list is serializable and implements the randomaccess.

This method also provides a convenient way to create a fixed-length list that is initialized to contain multiple elements:
List stooges = arrays.aslist ("Larry", "Moe", "Curly");
Parameters:
A-an array of supported lists.
Return:
Specifies the list view of the array.
See also:
Collection.toarray ()
Specific usage:
string[] arr = new string[] {"1", "2"};

List List = Arrays.aslist (arr);


Array->list (Stringarraytest.java)

Import Java.util.arrays;import Java.util.list;import java.util.ArrayList; public class stringarraytest{public   static void Main (string[] args)   {      string[] words = {"Ace", "Boom", "crew "," Dog "," Eon "};        list<string> wordList = arrays.aslist (words);        for (String e:wordlist)      {         System.out.println (e);}}   }

Rather silly practice
string[] words = {...}; list<string> list = new arraylist<string> (words.length); for (String s:words) {    list.add (s);}

Another method:

Import java.util.Collections; List myList = new ArrayList (); string[] myStringArray = new string[] {"Java", "is", "Cool"}; Collections.addall (MyList, myStringArray);




Conversion between array and list of Android development

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.