Java Basics Note (9) List collection----Collection

Source: Internet
Author: User

  1. Collection
    1. For the understanding of collections, a collection is a container for storing and managing objects of other objects
  2. Collection, first understand the parent interface of all collections----collection
    1. Feature: Store any object element
    2. Method
      1. Boolean Add (Object O)//add element o to the collection, success true, otherwise false
      2. Boolean AddAll (Collection C)//Add all elements from collection C to the current collection
      3. void clear ()//empties all elements in the current collection
      4. Boolean contains (object O)//Determine if object o exists in the current collection
      5. Boolean containsall (Collection C)//Determines whether the elements in set C exist in the current collection
      6. Boolean isEmpty ()//Determines whether the number of elements in the current collection is 0
      7. A Boolean remove (object o)//Removes the object o from the current collection and returns whether it is successful
      8. int size ()//Get the actual number of collection elements
      9. Object[] ToArray ()//Turn the set into the corresponding array
  3. List interface
    1. Features: Storing any object element, orderly, with subscript element content can be re-
    2. Method:
      1. Inherit all the methods in the parent interface collection
      2. void Add (int idx, Object obj)//Insert element at specified subscript idx position obj
      3. Object get (int idx)//Gets the element specified in the subscript idx position
      4. int indexOf (Object o)//returns o subscript in the current set, if not present returns-1
      5. Object Remove (int idx)//Deletes the element that specifies the subscript IDX position, and returns the element
      6. Object set (int idx,object o)//Modify the element of the IDX position to O, and return the pre-modified element
      7. List sublist (int beginidx, int toidx)//intercept subcollections, from subscript beginidx (inclusive) to TOIDX (not included)
    3. Traversal: Subscript traversal, foreach traversal, iterative traversal
      1. As follows:

Package com.lvsling.test;

Import java.util.ArrayList;

Import Java.util.Iterator;

Import java.util.List;

Public class testarraylist {

Public staticvoid main (string[] args) {

//student-- There are three properties, name,age,clazz

list<student> list = new arraylist<student> ();

Student S1 = new Student ("Jerry", "1");

List.add (S1);

List.add (new Student ("Tom", "2"));

List.add (new Student ("Mike", "1"));

        // Subscript Traversal

For (int i=0; i<list.size (); i++) {

System. out. println (List.get (i));

}

System. out. println ("-------------------------");

//ForEach Traverse

For (Object obj:list) {

Student s = (Student) obj;

System. out. println (S.getname ());

}

System. out. println ("-------------------------");

        // Iterative Traversal

Iterator it = List.iterator (); // gets the iterator for the collection object

While (It.hasnext ()) { // Determines if there is a next element

Object obj = It.next (); // Get Next element

System. out. println (obj);

}

}

}

    1. List Implementation Class
      1. ArrayList
        1. Array implementations, variable-length arrays
        2. Thread insecure, high efficiency
        3. Query fast, and delete slow
      2. Vector
        1. Array implementations, variable-length arrays
        2. Thread-safe, high-efficiency
      3. LinkedList
        1. Linked list implementation
        2. Query slow, and delete quickly
    1. Generic collection
      1. A type-safe collection that restricts the types of collection elements must be the same.
        1. generic type, consistent
        2. Generic types must be reference types (basic types use wrapper classes)
    2. Expansion and Promotion
      1. Tool class
        1. The collections class, which is a tool class, provides a set of static methods for manipulating a collection.
        2. Such as:
          1. Collections.sort (list)//in ascending order----NOTE: The collection element type must implement the Java.lang.Comaparable interface.
          2. Collections.reverse (list); Collection element Inversion
          3. Collections.shuffle (list); Random display of collection elements

Java Basics Note (9) List collection----Collection

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.