Conversion between arrays and lists in Android development: androidlist

Source: Internet
Author: User

Conversion between arrays and lists in Android development: androidlist
1. Convert the List into an array. (The List here indicates that the object is an ArrayList)
Call the toArray method of ArrayList.

 ToArray

Public <T> T [] toArray (T [] a) returns an array containing all 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.


Specified:

ToArray in Interface Collection <E>

Specified:
ToArray in interface List <E>

Overwrite:
ToArray in AbstractCollection <E>

Parameters:
A-array of list elements to be stored, if it is large enough; otherwise, it is a new array allocated for storing list elements with the same runtime type.
Return Value:
An array containing the list elements.
Throw:
ArrayStoreException-if the runtime type of a is not the super type of the runtime type of each element in this list.
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. Convert the array to 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 will be written directly to the array .) Together with Collection. toArray, this method serves as a bridge between array-based APIs and collection-based APIs. The returned list is serializable and implements RandomAccess.

This method also provides a convenient way to create a fixed-length list, which is initialized to contain multiple elements:
List stooges = Arrays. asList ("Larry", "Moe", "Curly ");
Parameters:
A-list arrays are supported.
Return Value:
List View of the specified array.
For more information, see:
Collection. toArray ()
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);      }   }}

 

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);





How does android convert the values in list to one-dimensional arrays in a loop?

Int [] array = new int [List. size ()]
For (int y = 0; y <List. size (); y ++ ){
Array [y] = imagesList. get (y );
}

JAVA/ANDROID array to LIST

String [] [] datas = {"aaa", "aa1", "aa11" },{ "bbb", "bb1", "bb11 "}, {"ccc", "PC3", "cc11 "},
{"Aaa", "aa2", "aa22" },{ "bbb", "bb2", "bb22" },{ "aaa", "aa3 ", "aa33 "},
{"Bbb", "bb3", "bb33 "}};

Map <String, DetailModel> map = new HashMap <String, DetailModel> ();
For (String [] strings: datas ){

DetailModel detail = new DetailModel ();

Detail. setPron (strings [0]);

List <MeanPhrase> li = new ArrayList <MeanPhrase> ();
MeanPhrase mean = new MeanPhrase ();
Mean. setMean (strings [1]);
Mean. setPhrase (strings [2]);
Li. add (mean );

Detail. setMpList (li );

// If the same key already exists, such as aaa, add an element to the list of objects corresponding to the key.
If (map. containsKey (detail. getPron ())){
Map. get (detail. getPron (). getMpList (). addAll (li );
} Else {// otherwise add directly
Map. put (detail. getPron (), detail );
}
}
// Convert map to list
List <DetailModel> resultList = new ArrayList <DetailModel> (map. values ());

For (DetailModel detailModel: resultList ){
System. out. println (detailModel );
}
// Output the values in the map and the number of group Result Records
System. out. println ("========================================== = ");
Set <String> keySet = map. keySet ();
For (String string: keySet ){
System. out. println (string + ":" + map. get (string). getP... the remaining full text>

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.