Java Basic Syntax--arraylist collection

Source: Internet
Author: User
Tags int size

1.1 creation of collections

To create a common format for a collection:

Guide Package:import java.util.ArrayList;

To create an object: is identical to other common reference data types, but specifies the type of data stored in the container: arraylist< The data type of the element to be stored > variable name = new arraylist< The data type of the element to store > ();

the elements stored in the collection can only be The data type element specified in the <> brackets; the data type in the "< data type to store element >" must be a reference data type, not 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

I NT

Integer

Long

Long

Float

Float

Double

Double

Char

Character

Boolean

Boolean

1.2 common methods in collections

the ArrayList collection provides some common methods:

function Description

method declaration

Boo Lean Add ( object obj "

will specify the element obj Append 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

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

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

ArrayList Collection provides some common methods to complement;

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 [].

1.3 traversal of a 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 :

 Packagecn.itcastdemo02;Importjava.util.ArrayList;//traversal of a collection Public classArraylist { Public Static voidMain (string[] args) {ArrayList<Integer> list =NewArraylist<integer>(); List.add (23); List.add (1); List.add (43); List.add (25); List.add (55); List.add (28);  for(inti = 0;i<list.size (); i++) {System.out.println (List.get (i)); }}}

Java Basic Syntax--arraylist 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.