[DataStructure] Five methods to init the List in java, javainit
Do you know how to init list in other way into T for new object? The following will give you serveral tips. If having other great idea, you are welcome to share.
[Java]View plaincopy
- Import java. util. ArrayList;
- Import java. util. Arrays;
- Import java. util. Collections;
- Import java. util. List;
- Public class ListUtil
- {
- /**
- * Init the List with different methods.
- *
- * @ Time Jul 21,201 4 5:39:10
- * @ Return void
- */
- Public static void init ()
- {
- // Use new to init List
- List <String> firstList = new ArrayList <String> (0 );
- FirstList. add ("ABC ");
- FirstList. add ("mcm ");
- FirstList. add ("CID ");
- System. out. println (firstList );
- // Use the string to init List
- List <String> testList = Arrays. asList ("ABC", "di-"," CID ");
- System. out. println (testList );
- // Output: [ABC, ABC, GFG, CID]
- /**
- * Notes: The following methods is dependent on the above List.
- */
- // Use arrays and copy to init List
- List <String> copyList = Arrays. asList (new String [testList. size ()]);
- Collections. copy (copyList, testList );
- System. out. println (copyList );
- // Use new object and copy to init List
- CopyList = new ArrayList <String> ();
- Collections. addAll (copyList, new String [testList. size ()]);
- Collections. copy (copyList, testList );
- System. out. println (copyList );
- // Use the list. subList to init List
- List <String> strSubList = testList. subList (0, testList. size ());
- System. out. println (strSubList );
- };
- Public static void main (String [] args)
- {
- ListUtil. init ();
- }
- }
The result is:
[Html]View plaincopy
- [ABC, di-, CID]
- [ABC, di-, CID]
- [ABC, di-, CID]
- [ABC, di-, CID]
- [ABC, di-, CID]
Java ant compilation error: init: compile: [javac] Compiling 1 source file to D: \ newjspworkspace \ ant02 \ build
It will take two days to understand.