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

Java ArrayList remove() Method

Java ArrayList remove() Method

Java ArrayList Java ArrayList

The remove() method is used to delete a single element from the dynamic array.

The syntax of the remove() method is:


// Delete specified element
arraylist.remove(Object obj)

// Delete element at specified index
arraylist.remove(int index)

**Note:** arraylist is an object of the ArrayList class.

Parameter Description:

If the obj element appears multiple times, the first occurrence of the element in the dynamic array is deleted.

Return Value

If an element is passed and deleted successfully, returns true.

If an index value is passed, returns the deleted element.

Note: If the specified index is out of range, the method will throw an IndexOutOfBoundsException.

Example

Deleting a specified element from the array:

Example

import

java.util.ArrayList

;

class

Main

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create an array

        ArrayList

<

String

sites

=

new

ArrayList

<>

(

)

;

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Taobao"

)

;

       

System

.

out

.

println

(

"Website list: "

sites

)

;

       

// Delete the element Taobao

       

boolean

result

=

sites.

remove

(

"Taobao"

)

;

       

System

.

out

.

println

(

"Is Taoabo deleted? "

result

)

;

       

System

.

out

.

println

(

"After using remove(): "

sites

)

;

   

}

}

The output of the above program is:


网站列表: [Google, Runoob, Taobao]
Taoabo 是否被删除? true
使用 remove() 后: [Google, Runoob]

In the example above, we used the remove() method to delete the element Taobao from the dynamic array.

Deleting an element from a specified position:

Example

import

java.util.ArrayList

;

class

Main

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create an array

        ArrayList

<

String

sites

=

new

ArrayList

<>

(

)

;

        sites.

add

(

"Google"

)

;

        sites.

add

(

"Runoob"

)

;

        sites.

add

(

"Taobao"

)

;

       

System

.

out

.

println

(

"Website list: "

sites

)

;

       

// Delete the element at index position 2

       

String

element

=

sites.

remove

(

2

)

;

       

System

.

out

.

println

(

"After using remove(): "

sites

)

;

       

System

.

out

.

println

(

"Removed element: "

element

)

;

   

}

}

The output of the above program is:


网站列表: [Google, Runoob, Taobao]
使用 remove() 后: [Google, Runoob]
移除的元素: Taobao

Deleting the first occurrence of an element:

Example

import

java.util.ArrayList

;

class

Main

{

   

public

static

void

main

(

String

[

]

args

)

{

       

// Create an array

        ArrayList

<

Integer

randomNumbers

=

new

ArrayList

<>

(

)

;

       

// Insert elements into the array

        randomNumbers.

add

(

22

)

;

        randomNumbers.

add

(

13

)

;

        randomNumbers.

add

(

35

)

;

        randomNumbers.

add

(

13

)

;

        randomNumbers.

add

(

40

)

;

       

System

.

out

.

println

(

"ArrayList: "

randomNumbers

)

;

       

// Delete the first occurrence of 13

       

boolean

result

=

randomNumbers.

remove

(

Integer

.

valueOf

(

13

)

)

;

       

System

.

out

.

println

(

"Is 13 deleted? "

result

)

;

       

System

.

out

.

println

(

"After using remove(): "

randomNumbers

)

;

   

}

}

The output of the above program is:


ArrayList: [22, 13, 35, 13, 40]
13 是否被删除? true
使用 remove() 后: [22, 35, 13, 40]

In the example above, we created a dynamic array named randomNumbers. In this array, the element 13 appears twice; note this line:

randomNumbers.remove(Integer.valueOf(13))

Integer.valueOf() converts 13 from int type to an Integer object. This is because the remove() method only takes an object as its parameter.

remove() is used to delete the first 13 that appears in the array.

Note: We can also use the clear() method to delete all elements in the array. To learn more, please visit Java ArrayList clear().

Java ArrayList Java ArrayList