Java Learning Path (vi): Collection

Source: Internet
Author: User
Tags array length

The origin of the collection
    • The length of the array is fixed, and when the added element exceeds the length of the array, it needs to be redefined
    • Java provides us with the collection class, can store arbitrary objects, the length can be changed. Increases as the element increases, decreasing as the element decreases
The difference between an array and a collection
    • Arrays can store both the base data type and the reference data type, the base data type stores the value, and the reference data type stores the address value
    • A collection can store only reference data types (object objects), and the base data types are stored in the collection, but are automatically boxed into objects when stored Eg:int==>integer
    • The array length is fixed and does not grow automatically
    • The length of the collection is variable and can grow depending on the element's increase

If the number of elements is fixed, we use the array

If the number of elements is not fixed, we use the collection

Collection Collection class:
    • List: An ordered set, indexed. In the same order as they are taken, you can repeat
      • ArrayList (Array implementation)
      • LinkedList (linked list implementation)
      • Vector (Array implementation)
    • Set: Unordered collection, no index. The order of deposit and fetch is different, can not repeat
      • HashSet (hashing algorithm)
      • TreeSet (binary tree algorithm)
The principle of the fractional group realization in ArrayList Collection Center

Eg: there is an initialized array with a capacity of 10, when it is not enough, it will automatically generate an array of 1.5 times times larger, the value is re-assigned, the previous small array of garbage collection

Several basic methods of collection
    • Boolean Add (e e) adds an element
    • Boolean remove (object o) removes an element
    • void Clear () empties this collection
    • Boolean contains (Object o)
    • Boolean IsEmpty () determines whether this collection is empty
    • int size ()
    • ToArray () Converts a collection to an array
    • Boolean AddAll (Collection C1) adds C1
    • Boolean RemoveAll (Collection C1) removes all elements from C1 in this
    • Boolean Containsall (Collection C1) Determines whether this contains all elements in the C1
    • Boolean Retainall (Collection C1) set
Traversing a collection, we use iterators

Iterators are used to iterate through each element of a collection.

Method: Hasnext () and Next ()

 PackageLesson3;//This is a small exampleImportjava.util.ArrayList;Importjava.util.Collection;ImportJava.util.Iterator; Public classNULL03 { Public Static voidMain (string[] args) {Collection C1=NewArrayList ();//that's not polymorphic? C1.add ("A"); C1.add (B); C1.add (C); Iterator Iterator=C1.iterator ();  while(Iterator.hasnext ()) {Object o=Iterator.next ();        System.out.println (o); }            }}

Java Learning Path (vi): 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.