Do you know how to init list in other way except for new object? The following will give you serveral tips. If having other great idea, you are welcome to share.
-
import java.util.ArrayList;
-
import java.util.Arrays;
-
import java.util.Collections;
-
import java.util.List;
-
-
public class ListUtil
-
{
-
-
-
-
-
-
-
-
public static void init()
-
{
-
-
List<String> firstList = new ArrayList<String>(0);
-
firstList.add("ABC");
-
firstList.add("FGF");
-
firstList.add("CID");
-
System.out.println(firstList);
-
-
-
List<String> testList = Arrays.asList("ABC", "FGF", "CID");
-
System.out.println(testList);
-
-
-
-
-
-
-
-
List<String> copyList = Arrays.asList(new String[testList.size()]);
-
Collections.copy(copyList,testList);
-
System.out.println(copyList);
-
-
-
copyList = new ArrayList<String>();
-
Collections.addAll(copyList, new String[testList.size()]);
-
Collections.copy(copyList,testList);
-
System.out.println(copyList);
-
-
-
List<String> strSubList = testList.subList(0, testList.size());
-
System.out.println(strSubList);
-
};
-
-
public static void main(String[] args)
-
{
-
ListUtil.init();
-
}
-
}
The result is :
-
[ABC, FGF, CID]
-
[ABC, FGF, CID]
-
[ABC, FGF, CID]
-
[ABC, FGF, CID]
-
[ABC, FGF, CID]
【DataStructure】 Five methods to init the List in java,布布扣,bubuko.com
【DataStructure】 Five methods to init the List in java
原文:http://blog.csdn.net/sxb0841901116/article/details/38548891