Java collection _ collection subinterface list method, ArrayList class embodiment, collectionarraylist

Source: Internet
Author: User

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 );*/}}

 

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.