Java Arrays. asList usage, javaarrays. aslist

Source: Internet
Author: User

Java Arrays. asList usage, javaarrays. aslist

  1. Import java. util. Arrays;
  2. Import java. util. List;
  3. /**
  4. *
  5. * This class demonstrates the asList method in the Arrays class.
  6. * Four paragraphs are used to demonstrate the features of the method.
  7. *
  8. * (1) this method does not support arrays of the basic data type. It is not recommended when arrays are of the basic data type.
  9. * (2) when the asList () method is used, the array is linked with the list.
  10. * When one of the updates is made, the other will automatically get the updates.
  11. * Note: The basic data type array does not have this attribute only for the object array type.
  12. * (3) the array obtained by asList does not have the add or remove methods.
  13. *
  14. * Reading: You can check the source code of the Arrays class. The List returned by asList is implemented in Array.
  15. * Internal class, but this class does not define the add and remove methods. In addition, why modify one of them, the other is automatically
  16. * Updated, because the asList obtains the List and actually references the array.
  17. */
  18. Public class AsListTest {
  19. Public static void main (String [] args ){
  20. /* Section 1: problems in using asList for basic data types */
  21. /* Note: Although JDK1.6 can convert an array of the basic data type to a List, it still has a defect */
  22. Int [] a_int = {1, 2, 3, 4 };
  23. /* The expected output should be 1, 2, 3, 4, but the output is actually just a reference. Here it treats a_int as an element */
  24. List a_int_List = Arrays. asList (a_int );
  25. Foreach (a_int_List );
  26. /* For this reason, we need to traverse the elements in this way */
  27. ForeachForBase (a_int_List );
  28. /* Section 2: Use asList for an array of object types as expected */
  29. Integer [] a_Integer = new Integer [] {1, 2, 3, 4 };
  30. List a_Integer_List = Arrays. asList (a_Integer );
  31. Foreach (a_Integer_List );
  32. /* Section 3: When the List after the array or asList is updated, the other will automatically get the update */
  33. A_Integer_List.set (0, 0 );
  34. Foreach (a_Integer_List );
  35. Foreach (a_Integer );
  36. A_Integer [0] = 5;
  37. Foreach (a_Integer_List );
  38. Foreach (a_Integer );
  39. /* Section 4: For an array of basic types, after modifying the corresponding value in the List after asList, an exception is reported during running.
  40. * However, the List corresponding to the basic type array will change, which is undoubtedly
  41. */
  42. /*
  43. * A_int_List.set (0, 0 );
  44. * Foreach (a_int_List); foreach (a_int );
  45. */
  46. A_int [0] = 5;
  47. ForeachForBase (a_int_List );
  48. Foreach (a_int );
  49. }
  50. /* Print method */
  51. Private static void foreach (List list ){
  52. For (Object object: list ){
  53. System. out. print (object + "");
  54. }
  55. System. out. println ();
  56. }
  57. Private static void foreachForBase (List a_int_List ){
  58. Int [] _ a_int = (int []) a_int_List.get (0 );
  59. Foreach (_ a_int );
  60. }
  61. Private static void foreach (int [] a_int ){
  62. For (int I: a_int ){
  63. System. out. print (I + "");
  64. }
  65. System. out. println ();
  66. }
  67. Private static void foreach (Integer [] _ a_Integer ){
  68. For (int I: _ a_Integer ){
  69. System. out. print (I + "");
  70. }
  71. System. out. println ();
  72. }
  73. }

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.