Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

Java ArrayList

Java ArrayList

Java Collections Framework At this point, the trimToSize() method can remove the unallocated space and change the capacity of the ArrayList so that it equals the number of elements in the ArrayList.

Java Collections Framework

The ArrayList class is a dynamically modifiable array. The difference from a normal array is that it has no fixed-size limit; we can add or delete elements.

ArrayList extends AbstractList and implements the List interface.


import java.util.ArrayList; // 引入 ArrayList 类

ArrayList objectName =new ArrayList<>();  // 初始化

: the object name.

Adding elements

ArrayList is an array queue that provides related functions such as adding, deleting, modifying, and traversing.

Example

import

java.util.ArrayList

;

public

class

RunoobTest

{

   

public

static

void

main

(

String

[

]

args

)

{

        ArrayList

<

String

sites

=

new

ArrayList

<

String

(

)

;

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Taobao"

)

;

        sites.

add

(

"Weibo"

)

;

       

System

.

out

.

println

(

sites

)

;

   

}

}

The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:

[Google, Runoob, Taobao, Weibo]

Accessing elements

The execution output of the above example is:

Example

import

java.util.ArrayList

;

public

class

RunoobTest

{

   

public

static

void

main

(

String

[

]

args

)

{

        ArrayList

<

String

sites

=

new

ArrayList

<

String

(

)

;

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Taobao"

)

;

        sites.

add

(

"Weibo"

)

;

       

System

.

out

.

println

(

sites.

get

(

1

)

)

;

 

To access elements in an ArrayList you can use the get() method:

   

}

}

Note// Access the second element

The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:

Runoob

: the array index value starts from 0.

Modifying an element

Example

import

java.util.ArrayList

;

public

class

RunoobTest

{

   

public

static

void

main

(

String

[

]

args

)

{

        ArrayList

<

String

sites

=

new

ArrayList

<

String

(

)

;

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Taobao"

)

;

        sites.

add

(

"Weibo"

)

;

        sites.

set

(

2

,

"Wiki"

)

;

To modify an element in an ArrayList you can use the set() method. The set(int index, E element) method's first parameter is the index, indicating the position of the element to be replaced, and the second parameter is the new element, indicating the new value to be set:

       

System

.

out

.

println

(

sites

)

;

   

}

}

The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:


[Google, Runoob, Wiki, Weibo]

Delete elements

// The first parameter is the index position, the second is the value to be modified

Example

import

java.util.ArrayList

;

public

class

RunoobTest

{

   

public

static

void

main

(

String

[

]

args

)

{

        ArrayList

<

String

sites

=

new

ArrayList

<

String

(

)

;

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Taobao"

)

;

        sites.

add

(

"Weibo"

)

;

        sites.

remove

(

3

)

;

To delete an element in an ArrayList you can use the remove() method:

       

System

.

out

.

println

(

sites

)

;

   

}

}

The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:


[Google, Runoob, Taobao]

// Delete the fourth element

Calculating the size

Example

import

java.util.ArrayList

;

public

class

RunoobTest

{

   

public

static

void

main

(

String

[

]

args

)

{

        ArrayList

<

String

sites

=

new

ArrayList

<

String

(

)

;

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Taobao"

)

;

        sites.

add

(

"Weibo"

)

;

       

System

.

out

.

println

(

sites.

size

(

)

)

;

   

}

}

The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:


4

To calculate the number of elements in an ArrayList you can use the size() method:

Iterating over the array list

Example

import

java.util.ArrayList

;

public

class

RunoobTest

{

   

public

static

void

main

(

String

[

]

args

)

{

        ArrayList

<

String

sites

=

new

ArrayList

<

String

(

)

;

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Taobao"

)

;

        sites.

add

(

"Weibo"

)

;

       

for

(

int

i

=

0

;

i

<

sites.

size

(

)

;

i

++

)

{

           

System

.

out

.

println

(

sites.

get

(

i

)

)

;

       

}

   

}

}

The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:


Google
Runoob
Taobao
Weibo

We can use for to iterate over the elements in the array list:

Example

import

java.util.ArrayList

;

public

class

RunoobTest

{

   

public

static

void

main

(

String

[

]

args

)

{

        ArrayList

<

String

sites

=

new

ArrayList

<

String

(

)

;

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Taobao"

)

;

        sites.

add

(

"Weibo"

)

;

       

for

(

String

i

:

sites

)

{

           

System

.

out

.

println

(

i

)

;

       

}

   

}

}

The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:


Google
Runoob
Taobao
Weibo

We can also use for-each to iterate over the elements:

Other reference types

The elements in an ArrayList are actually objects. In the example above, the array list elements are all of the String type. If we want to store other types, and

can only be a reference data type, then we need to use the wrapper classes of the primitive types.

Basic types Reference type
boolean Boolean
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character

The wrapper class table corresponding to primitive types is as follows:

ArrayList li=new ArrayList<>();     // 存放整数元素
ArrayList li=new ArrayList<>();   // 存放字符元素

In addition, BigInteger and BigDecimal are used for high-precision calculations. BigInteger supports arbitrary-precision integers and is also a reference type, but they have no corresponding primitive types.

Example

import

java.util.ArrayList

;

public

class

RunoobTest

{

   

public

static

void

main

(

String

[

]

args

)

{

        ArrayList

<

Integer

myNumbers

=

new

ArrayList

<

Integer

(

)

;

        myNumbers.

add

(

10

)

;

        myNumbers.

add

(

15

)

;

        myNumbers.

add

(

20

)

;

        myNumbers.

add

(

25

)

;

       

for

(

int

i

:

myNumbers

)

{

           

System

.

out

.

println

(

i

)

;

       

}

   

}

}

The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:


10
15
20
25

The following example uses an ArrayList to store numbers (using the Integer type):

ArrayList Sorting

The Collections class is also a very useful class, located in the java.util package; its provided sort() method can sort lists of characters or numbers.

Example

import

java.util.ArrayList

;

import

java.util.Collections

;

 

The following example sorts letters:

public

class

RunoobTest

{

   

public

static

void

main

(

String

[

]

args

)

{

        ArrayList

<

String

sites

=

new

ArrayList

<

String

(

)

;

        sites.

add

(

"Taobao"

)

;

        sites.

add

(

"Wiki"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Weibo"

)

;

        sites.

add

(

"Google"

)

;

       

Collections

.

sort

(

sites

)

;

 

// Import the Collections class

       

for

(

String

i

:

sites

)

{

           

System

.

out

.

println

(

i

)

;

       

}

   

}

}

The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:


Google
Runoob
Taobao
Weibo
Wiki

// Sort letters

Example

import

java.util.ArrayList

;

import

java.util.Collections

;

 

The following example sorts letters:

public

class

RunoobTest

{

   

public

static

void

main

(

String

[

]

args

)

{

        ArrayList

<

Integer

myNumbers

=

new

ArrayList

<

Integer

(

)

;

        myNumbers.

add

(

33

)

;

        myNumbers.

add

(

15

)

;

        myNumbers.

add

(

20

)

;

        myNumbers.

add

(

34

)

;

        myNumbers.

add

(

8

)

;

        myNumbers.

add

(

12

)

;

       

Collections

.

sort

(

myNumbers

)

;

 

The following example sorts numbers:

       

for

(

int

i

:

myNumbers

)

{

           

System

.

out

.

println

(

i

)

;

       

}

   

}

}

The ArrayList class provides many useful methods; to add elements to an ArrayList you can use the add() method:


8
12
15
20
33
34

// Sort numbers

Java ArrayList Methods

Method Description
add() The list of commonly used Java ArrayList methods is as follows:
addAll() Insert an element into the arraylist at the specified position
clear() Add all elements from a collection to the arraylist
clone() Delete all elements in the arraylist
contains() Copy an arraylist
get() Determine whether an element is in the arraylist
indexOf() Get an element from the arraylist by index value
removeAll() Return the index value of an element in the arraylist
remove() Delete all elements in the arraylist that exist in a specified collection
size() Delete a single element in the arraylist
isEmpty() Return the number of elements in the arraylist
subList() Determine whether the arraylist is empty
set() Intercept part of the arraylist elements
sort() Replace the element at the specified index in the arraylist
toArray() Sort the arraylist elements
toString() Convert the arraylist into an array
ensureCapacity() Convert the arraylist into a string
lastIndexOf() Set the arraylist to a specified capacity
retainAll() Return the last occurrence position of a specified element in the arraylist
containsAll() Retain those elements in the arraylist that also exist in a specified collection
trimToSize() Check whether the arraylist contains all elements of a specified collection
removeRange() Adjust the capacity in the arraylist to the number of elements in the array
replaceAll() Delete the elements that exist between specified indices in the arraylist
removeIf() Replace every element in the array with the given operation content
forEach() Delete all arraylist elements that satisfy a specific condition

Traverse every element in the arraylist and perform a specific operationhttps://www.runoob.com/manual/jdk11api/java.base/java/util/ArrayList.html

Java Collections Framework At this point, the trimToSize() method can remove the unallocated space and change the capacity of the ArrayList so that it equals the number of elements in the ArrayList.