Java basics-ArrayList and listparts list (1), arraylistparts list

Source: Internet
Author: User
Tags addall

Java basics-ArrayList and listparts list (1), arraylistparts list

I. Definition

ArrayList and referlist are two collection classes used to store a series of object references ).

The referenced formats are:

1 ArrayList <String> list = new ArrayList <String> ();

1 items list <Integer> list = new items list <Integer> ();

Ii. Differences between ArrayList and rule list

1. ArrayList implements a dynamic array-based data structure. The ArrayList is based on the linked list data structure;

2. for Random Access to get and set, ArrayList takes precedence over sorted list. Because the pointer list needs to move the pointer.

3. For add and remove operations, the sort list is dominant because ArrayList needs to move data.

(About 3, there is another debate on the Internet, which means that the effect of adding or deleting large data, ending or adding or deleting large data is different. It is to be verified !)

Iii. Common Methods of ArrayList and rule list

Code example:

1 import java. util. arrayList; 2 3 public class CollT2 {4 5 public static void main (String [] args) {6 7 // create an ArrayList 8 ArrayList <String> list = new ArrayList <String> (); 9 System. out. println ("initialization size:" + list. size (); 10 11 // Add element 12 list. add ("Beijing"); 13 list. add ("Tianjin"); 14 list. add ("Shanghai"); 15 list. add ("Guangzhou"); 16 list. add ("Shenzhen"); 17 list. add ("Haikou"); 18 list. add ("Xiamen"); 19 System. out. println ("current capacity:" + list. size (); 20 21 // the size of the ArrayList is the same as the size of the actually contained element. 22 // note: this operation demonstrates the exception thrown below: java. util. concurrentModificationException23 list. trimToSize (); 24 25 // traverse 26 for (String string: list) {27 System. out. println (string); 28 29 // insert element 30 list at the specified position. add (2, "Heilongjiang"); 31 for (String string1: list) {32 System. out. println (string1); 33} 34 System. out. println ("======== split line ======"); 35 36 // clear the list37 list. clear (); 38 for (String string3: list) {39 System. out. println (string3); 40} 41 42} 43} 44}

1 import java. util. writable list; 2 3 // ArrayList is implemented based on arrays, so it has the characteristics of arrays, that is, the query speed is fast, but the modification and insertion speed is a little slow. 4 // The sorted list to be introduced below is used to solve this problem. Sorted list is based on the linked list and complementary with ArrayList. 5 // in actual development, we should decide which one to use based on our own needs. 6 7 // The bottom layer of the sort list adopts a bidirectional loop list, which provides a high speed for insert and delete operations. 8 // we can also use consumer list to implement queue and stack 9 10 public class CollT3 {11 @ SuppressWarnings ("null") 12 public static void main (String [] args) {13 14 // create a list 15 Response list <Integer> list = new Response list <Integer> (); 16 Response list <Integer> list2 = new Response list <Integer> (); 17 listing list <Integer> list3 = new listing list <Integer> (); 18 listing list <Integer> list4 = n Ew rule list <Integer> (); 19 20 System. out. println (list. size (); 21 22 // Add element 23 // list. add ("pot meat"); the data type is not considered 24 // list. add ("slide segment"); 25 System. out. println ("==== split line 1 ===="); 26 27 list. add (5); 28 list. add (6); 29 list. add (7); 30 list. add (8); 31 list. add (9); 32 list. add (10); 33 34 list2.add (-1); 35 list2.add (-2); 36 list2.add (-3); 37 list2.add (-4 ); 38 list2.add (-5); 39 40 list3.add (111); 41 list3.add (2 22); 42 list3.add (333); 43 list3.add (444); 44 list3.add (555); 45 46 list4 = null; 47 48 System. out. println (list. size (); 49 50 // traverse 51 for (Integer a: list) {52 System. out. println (a); 53} 54 55 list. add (4, 11111); 56 System. out. println (list); // The 57 System is printed horizontally. out. println ("=== split line 2 ==="); 58 59 list. add (3, 22222); 60 System. out. println (list); // The result of secondary verification is the 61 62 System. out. println ("=== split line 3 = "); 63 System. out. println (list. addAll (list2); 64 System. out. println (list); 65 66 System. out. println ("= split line 4 ="); 67 // error: System. out. println (2, list. addAll (list3); 68 list. addAll (2, list3); // insert all elements in the specified collection from the specified position to the list. 69 System. out. println (list); 70 71 System. out. println ("= split line 5 ="); 72 list3.addFirst (3); // print 73 System separately. out. println (list3); 74 75 System. out. println ("=== split line 6 ==="); 76 list3.addLast (0000000001); // 1 77 list3.addLast (000000000); // 0 78 list3.addLast (0 ); // 0 79 // list3.addLast (00000000000000009); // The literal 00000000000000009 of type int is out of range 80 list3.addLast (0000000000000000000000000 000000000007); // the end can only be written to, 8, and 9, but not 81 System. out. println (list3); 82 83 list. clear (); 84 System. out. println (list); // [] 85 86 System. out. println ("= split line 7 ="); 87 88 list2.clone (); // returns a superficial copy of the shard list. 89 System. out. println (list); 90 91 System. out. println ("= split line 8 ="); 92 // list3.contains (999999999); // er, it's not so verified !!!! 93 System. out. println (list3.contains (999999999); // returns true if the list contains the specified element. 94 95 System. out. println ("= split line 9 ="); 96 // list3.element (); // er, it's not so verified !!!! 97 System. out. println (list3.element (); // gets the header of the list but does not remove it (the first element ). 98 System. out. println (list3); // verify that the above operation is true only for obtaining 99 100 System not removed. out. println ("= 10 ="); // pay attention to the list3 operation, and add 101 System to its header. out. println (list3.get (2); // returns the element at the specified position in this list. 102 103 System. out. println ("= 11 ="); 104 System. out. println (list3.getFirst (); // returns the first element of this list. 105 System. out. println (list3.getLast (); // return the last element of this list. 106 107 System. out. println ("= split line 12 ="); 108 System. out. println (list3.indexOf (333); // returns the index of the specified element that appears for the first time in this list. If this element is not included in this list,-1 is returned. 109 System. out. println (list3.indexOf (10); 110 111 System. out. println ("= split line 13 ="); 112 list3.add (333); 113 list3.add (333); 114 list3.add (333); 115 System. out. println (list3); 116 System. out. println (list3.lastIndexOf (0); // returns the index of the last specified element in this list. If this element is not included in this list,-1 is returned. 117 System. out. println (list3.lastIndexOf (9); //-1118 119 System. out. println ("= split line 14 ="); 120 list3.offer (6); // Add the specified element to the end of the list (the last element ). 121 System. out. println (list3.offer (6); // true122 System. out. println (list3); 123 124 System. out. println ("= split line 15 ="); 125 System. out. println (list3.peek (); // gets the header of this list but does not remove it (the first element ). 126 127 // peekFirst (); obtain but not remove the last element of this list; if this list is empty, null is returned. 128 // System. out. println (list4.peekFirst (); list4 is defined as null, and an error is reported in yellow. @ SuppressWarnings ("null") is used "). Verification: java. lang. nullPointerException129 // System. out. println (list4.peekLast (); java. lang. nullPointerException130 131 System. out. println ("= 16 ="); 132 System. out. println (list3), 133 list3.poll (); 134 System. out. println (list3); 135 136 System. out. println ("=== split line 17 ==="); 137 System. out. println (list3); 138 list3.toArray (); // returns an array containing all elements in the list in the appropriate order (from the first element to the last element. 139 System. out. println (list3); 140} 141 142}

Print result:

0 === split line 1 === 65678910 [5, 6, 7, 8, 11111, 9, 10] === split line 2 === [5, 6, 7, 22222, 8, 11111, 9, 10] === split line 3 === true [5, 6, 7, 22222, 8, 11111, 9, 10,-1,-2,-3,-4,-5] === split line 4 === [5, 6,111,222,333,444,555, 7, 22222, 8, 11111, 9, 10,-1,-2,-3,-4, -5] === split line 5 === [3,111,222,333,444,555] === split line 6 === [3,111,222,333,444,555, 1, 0, 0, 7] [] = split line 7 = [] = split line 8 = false = split line 9 = 3 [3,111,222,333,444,555, 1, 0, 0, 7] === split line 10 ==== 222 === split line 11 ==== 37 === split line 12 === 3-1 === split line 13 = === [3,111,222,333,444,555, 1, 0, 0, 7,333,333,333] 8-1 = split line 14 = true [3,111,222,333,444,555, 1, 0, 0, 7,333,333,333, 6, 6] === split line 15 ===== 3 === split line 16 === [3,111,222,333,444,555, 1, 0, 0, 7,333,333,333, 6, 6] [111,222,333,444,555, 1, 0, 0, 7,333,333,333, 6, 6] === split line 17 === [111,222,333,444,555, 1, 0, 0, 7,333,333,333, 6, 6] [111,222,333,444,555, 1, 0, 0, 7,333,333,333, 6, 6]

 

Related Article

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.