List common operations for the Java collection

Source: Internet
Author: User

I. Definition
Collections (Collection) are a common way of storing objects in Java, and each object in the collection is ultimately stored in a similar array []. So why do arrays have to use collections? Because the length in the collection is variable, you do not have to define its length at the beginning (the array must define its length when it is defined), the array can store the base data type, and the collection can store arbitrary objects.
The Java Collection (collection) contains list and set; This chapter first describes the list collection
Two. List category
ListCommonCan also include: Arraylit,linkedlist,vecor;

Three. List common operations
The following is a list of common ways to do this with ArrayList.

  • A list belongs to one of the set, its length is variable, the value is variable, and the content can be any object

      • The list result is an array-like format and can also be seen as a list in a similar Pyhotn
      • The list is ordered and its value can be repeated, so it can be manipulated using an index
      • The set is unordered, so its value cannot be duplicated.

    Methods in the list
    Generating ArrayList objects
    ArrayList ArrayList = new ArrayList ();
    1. Increase
    Arraylist.add () is added by default at the last location
    Arraylist.add (int index,object obj) is added at the specified location
    2. By deleting
    Arraylist.remove (int index) is removed by index
    Arraylist.remove (object) is removed according to the value object, and if there are multiple identical objects, the first one is deleted sequentially.
    3. Change
    Arraylist.set (int index,object obj) sets its value by the specified index
    4. Check
    Arraylist.get (int index) gets the corresponding value by index
    Arraylist.index (Object obj) gets the corresponding index value according to the specific object, and if the same object gets the first corner label
    Arraylist.lastindex (Object obj) gets the corresponding last index value according to the specific object (reverse arrangement)
    5. Traversing the list
    5.1 Using a For loop
    for (int i=0;i<arraylist.size (); i++) {
    System.out.println (Arraylist1.get (i));
    }
    5.2 Using Iterators iterator
    Iterator it = Arraylist.iterator ();
    5.2.1 method 1s using a for loop
    for (; It.hasnext ();) {
    System.out.println (It.next ());
    }

    5.2.2 Method 2 using the while loop
    while (It.hasnext ()) {
    System.out.println (It.next ());
    }

6. Compare
7. Judging

List common operations for the Java 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.