Java-Foundation-16th Chapter ArrayList Collection

Source: Internet
Author: User
Tags array length int size

ArrayList Collection

The JDK provides a series of special classes that can store any type of element and are variable in length, collectively referred to as a collection.

The ArrayList collection is the most common collection in a program, which belongs to the reference data type (Class). A variable-length array is encapsulated inside the ArrayList, and when the deposited element exceeds the array length, ArrayList allocates a larger array in memory to store the elements, so the ArrayList collection can be thought of as a variable-length array.

creation of collections

Guide package: Import java.util.ArrayList;

Create objects: Exactly the same way as other normal reference data types, but specify the data types stored in the container:

arraylist< the data type of the element to be stored > variable name = new arraylist< the data type to store the element > ();

L The elements stored in the collection can only be specified in <> parentheses in the data type element;

Thedata type in the < data type > to store element must be a reference data type and cannot be a basic data type ;

The following is a representation of the reference data type for the 8 basic data types:

Basic data types

The corresponding reference data type representation

Byte

Byte

Short

Short

Int

Integer

Long

Long

Float

Float

Double

Double

Char

Character

Boolean

Boolean

Cases:

L Storing elements of type string

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

L store data of type int

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

L Store Phone-type data

L arraylist<phone> list = new arraylist<phone> ();

common methods in collections

Method declaration

Function description

Boolean Add (Object obj )

Appends the specified element obj to the end of the collection

Object Get (int index )

Returns the element at the specified position in the collection

int size ()

Returns the number of elements in the collection

Code Demo:

Importjava.util.ArrayList; Public classArrayListDemo01 { Public Static voidMain (string[] args) {//Create ArrayList Collectionarraylist<string> list =NewArraylist<string>(); //adding elements to the collectionList.add ("STU1"); List.add ("STU2"); List.add ("Stu3"); List.add ("Stu4"); //gets the number of elements in the collectionSystem.out.println ("Length of the collection:" +list.size ()); //Remove and print the element at the specified positionSystem.out.println ("The 1th element is:" + list.get (0)); System.out.println ("The 2nd element is:" + list.get (1)); System.out.println ("The 3rd element is:" + list.get (2)); System.out.println ("The 4th element is:" + list.get (3)); }}

The ArrayList collection is equivalent to a variable-length array, so accessing the elements in the collection is also indexed, the first element is stored at index 0, the second element is stored in the position of index 1, and so on.

traversal of a collection

By iterating through the collection, you get each element in the collection, which is the most common operation in the collection. The traversal of a collection is much like the traversal of an array, and is indexed by the way the collection is traversed as follows:

13Importjava.util.ArrayList;14 Public classArrayListDemo02 {15 Public Static voidMain (string[] args) {16//Create ArrayList Collectionarraylist<integer> list =NewArraylist<integer>(); 18//adding elements to a collectionList.add (13); List.add (15); List.add (22); List.add (29); 23//iterating through the collection24 for(inti = 0; I < list.size (); i++) { 25//get to each element in the collection by index26intn =List.get (i);27System.out.println (n);28            } 29        } 30}

The type of the return value of the Get method is the type of the element in the collection.

Common methods supplement in a collection

Method declaration

Function description

Boolean Add (int index, Object obj )

Inserts the specified element, obj, into the specified position in the collection

Object remve (int index )

Removes the element at the specified index from the collection, returning the element

void Clear ()

Empties all elements in the collection

Object Set (int index, Object obj )

Replaces the element at the specified position in the collection with the specified element, obj

boolean Add (int index, Object obj)

Function: Specify the index position in the collection, add a new element, obj

Function Description: Assume that the collection list has elements ["Java", "ee"], when using Add (1, "Javaweb"), the elements in the collection list are ["Java", "Javaweb", "Java ee"].

Object Set (int index, object obj)

Function: Replaces the element at the specified index position in the collection with the specified element, obj

Function Description: Assume that the collection list has elements ["Java", "ee"], when using Set (0, "Javaweb"), the elements in the collection list are ["Javaweb", "Java ee"].

Object remve (int index)

Function: Removes the element at the specified index from the collection, returning the element

Function Description: Assume that the collection list has elements ["Java", "ee"], when you use Remove (0), the elements in the collection list are ["Java ee"], and the return value is "JavaScript".

void Clear ()

Function: Empties all elements in the collection

Function Description: Assume that the collection list has elements ["Java", "EE"], and when clear () is used, the elements in the collection list are empty [].

Java-Foundation-16th Chapter ArrayList Collection

Related Article

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.