[DataStructure] Five methods to init the List in java, javainit

Source: Internet
Author: User

[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
  1. Import java. util. ArrayList;
  2. Import java. util. Arrays;
  3. Import java. util. Collections;
  4. Import java. util. List;
  5. Public class ListUtil
  6. {
  7. /**
  8. * Init the List with different methods.
  9. *
  10. * @ Time Jul 21,201 4 5:39:10
  11. * @ Return void
  12. */
  13. Public static void init ()
  14. {
  15. // Use new to init List
  16. List <String> firstList = new ArrayList <String> (0 );
  17. FirstList. add ("ABC ");
  18. FirstList. add ("mcm ");
  19. FirstList. add ("CID ");
  20. System. out. println (firstList );
  21. // Use the string to init List
  22. List <String> testList = Arrays. asList ("ABC", "di-"," CID ");
  23. System. out. println (testList );
  24. // Output: [ABC, ABC, GFG, CID]
  25. /**
  26. * Notes: The following methods is dependent on the above List.
  27. */
  28. // Use arrays and copy to init List
  29. List <String> copyList = Arrays. asList (new String [testList. size ()]);
  30. Collections. copy (copyList, testList );
  31. System. out. println (copyList );
  32. // Use new object and copy to init List
  33. CopyList = new ArrayList <String> ();
  34. Collections. addAll (copyList, new String [testList. size ()]);
  35. Collections. copy (copyList, testList );
  36. System. out. println (copyList );
  37. // Use the list. subList to init List
  38. List <String> strSubList = testList. subList (0, testList. size ());
  39. System. out. println (strSubList );
  40. };
  41. Public static void main (String [] args)
  42. {
  43. ListUtil. init ();
  44. }
  45. }

The result is:

[Html]View plaincopy
  1. [ABC, di-, CID]
  2. [ABC, di-, CID]
  3. [ABC, di-, CID]
  4. [ABC, di-, CID]
  5. [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.


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.