List-–> Array
In the development of people should often encounter List
with the array types of mutual conversion, a simple 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 arra Ylist ();
List.add ("Wang Lihu");
List.add ("John");
List.add ("Dick");
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 listed above, when you want to ArrayList
convert the type of data to String[]
, you have to List
traverse the type, in fact, does not need to List
provide us with a good way to solve the list conversion into an array of problems, not to 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 ("John");
List.add ("Dick");
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, it's simple to ArrayList
provide public T[] toArray(T[] a)
a way to return an array that contains all the elements in the list in the correct order, and the Run-time type of the array is the Run-time type of the specified array. If the list can be placed in 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 have the remaining space (that is, the elements of the array are more than the list), the element that is immediately at the end of the collection is set to the array null
. This is useful for determining the length of a list, but only when callers know that the list does not contain any null
elements.
Array->list
So how do you convert arrays into? List
Let's look at a small example, as follows:
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]= "John";
Array[2]= "Dick";
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));
}
Did you find it very troublesome? In fact the array conversion becomes List
the problem Arrays
object also provided to us public static List asList(T… a)
for us to invoke, try running the following example:
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]= "John";
Array[2]= "Dick";
list<string> list=arrays.aslist (array);
for (int i=0;i<list.size (); i++) {
System.out.println (List.get (i));
}
Simply, the asList
method returns a fixed-size list supported by the specified array, which Collection.toArray
, together, serves as a bridge between an array-based API and a collection API. The returned list is serializable and implemented RandomAccess
. In addition, this method provides a convenient way to create a fixed-length list that is initialized to include 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", "John "," Dick ");
for (int i=0;i<list.size (); i++) {
System.out.println (List.get (i));}}}
Summarize
These are all the ArrayList and arrays of Android, and hopefully the content of this article will help you develop Android. If you have questions, you can leave a message for discussion.