Java collection _ collection subinterface list method, ArrayList class embodiment, collectionarraylist
/* Collection | -- List: the elements are ordered and can be repeated. Because the collection system has indexes. | -- ArrayList: the underlying data structure uses an array structure. Feature: Fast query speed. However, adding, deleting, and deleting are slow. The thread is not synchronized. | -- Linked list: the underlying linked list data structure. Features: Fast addition, deletion, and slow query. The thread is not synchronized. | -- Vector: The underlying layer is the array data structure. Thread synchronization. Replaced by ArrayList. Low efficiency. | -- Set: elements are unordered and cannot be duplicated., List: special method. Any method that can operate the badge is unique to the system. Add (index, element); addAll (index, Collection); Delete remove (index); change set (index, element); query get (index): subList (from, to); listIterator (); int indexOf (obj): gets the position of the specified element. ListIterator listIterator (); List set unique iterator. ListIterator is a subinterface of Iterator. During iteration, elements in the set cannot be operated through the method of the Set object. ConcurrentModificationException occurs. Therefore, when performing an Iterator, you can only use the Iterator to let go of the operation elements, but the Iterator method is limited. You can only judge, retrieve, and delete the elements, if you want to add or modify other operations, you need to use its sub-interface, ListIterator. This interface can only be obtained through the listIterator method of the List set. */Import java. util. *; class ListDemo {public static void sop (Object obj) {System. out. println (obj);} public static void method () {ArrayList al = new ArrayList (); // Add the element al. add ("java01"); al. add ("java02"); al. add ("java03"); sop ("the original set is:" + al); // add an element at a specified position. Al. add (1, "java09"); // Delete the element at the specified position. // Al. remove (2); // modify the element. // Al. set (2, "java007"); // obtain the element through the badge. Sop ("get (1):" + al. get (1); sop (al); // get all elements. For (int x = 0; x <al. size (); x ++) {System. out. println ("al (" + x + ") =" + al. get (x);} Iterator it = al. iterator (); while (it. hasNext () {sop ("next:" + it. next ();} // obtain the object location through indexOf. Sop ("index =" + al. indexOf ("java02"); List sub = al. subList (1, 3); sop ("sub =" + sub);} public static void main (String [] args) {// demo list iterator. ArrayList al = new ArrayList (); // Add the element al. add ("java01"); al. add ("java02"); al. add ("java03"); sop (al); ListIterator li = al. listIterator (); // sop ("hasPrevious ():" + li. hasPrevious (); while (li. hasNext () {Object obj = li. next (); if (obj. equals ("java02") // li. add ("java009"); li. set ("java006");} while (li. hasPrevious () {sop ("pre:" + li. previous ();} // sop ("hasNext ():" + li. hasNext (); // sop ("hasPrevio Us (): "+ li. hasPrevious (); sop (al);/* // you are prepared to add or delete elements during iteration. Iterator it = al. iterator (); while (it. hasNext () {Object obj = it. next (); if (obj. equals ("java02") // al. add ("java008"); it. remove (); // deletes the reference of Java 02 from the collection. Sop ("obj =" + obj);} sop (al );*/}}