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

Java ArrayList indexOf() Method

Java ArrayList indexOf() Method

Java ArrayList Java ArrayList

The indexOf() method returns the index value of an element in the dynamic array.

The syntax of the indexOf() method is:


arraylist.indexOf(Object obj)

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

Parameter Description:

Return Value

Returns the index value of the position of the specified element from the dynamic array.

If the obj element appears repeatedly in the dynamic array, returns the index of the first occurrence of obj in the array.

If the specified element does not exist in the dynamic array, the indexOf() method returns -1.

Example

Getting the index of a dynamic array element:

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

)

;

       

// Find the element at position index Runoob

       

int

position1

=

sites.

indexOf

(

"Runoob"

)

;

       

System

.

out

.

println

(

"The index position of Runoob: "

position1

)

;

       

// Find the element at position index Weibo

       

int

position2

=

sites.

indexOf

(

"Weibo"

)

;

       

System

.

out

.

println

(

"The index position of Weibo: "

position2

)

;

   

}

}

The output of the above program is:


Runoob 的索引位置: 1
Weibo 的索引位置: -1

In the example above, we created a dynamic array named sites.

Note these expressions:

// 元素存在于动态数组中,返回 1
sites.indexOf("Runoob");

//Element does not exist in the dynamic array, return -1
sites.indexOf("Weibo");

Getting the first occurrence position of an element:

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"

)

;

        sites.

add

(

"Runoob"

)

;

       

System

.

out

.

println

(

"Website list: "

sites

)

;

       

// Find the element at position index Runoob

       

int

position1

=

sites.

indexOf

(

"Runoob"

)

;

       

System

.

out

.

println

(

"The index position of Runoob: "

position1

)

;

   

}

}

The output of the above program is:


网站列表: [Google, Runoob, Taobao, Runoob]
Runoob 的索引位置: 1

In the example above, we created a dynamic array named sites. Here we used the indexOf() method to get the position of the Runoob element.

Runoob appears at two different positions in the array list; in this case, the method returns the index of the first occurrence of Runoob (i.e., 1).

If we want to get the last occurrence position of Runoob, we can use the lastIndexOf() method. To learn more, please visit Java ArrayList lastIndexOf().

**Note:** We can also use Java ArrayList get() the method to get the element at the specified index.

Java ArrayList Java ArrayList