logo

Método indexOf() da lista Java

O método indexOf() da interface List retorna o índice da primeira ocorrência do elemento especificado nesta lista. Retorna -1 se o elemento especificado não estiver presente nesta lista.

Sintaxe

 public int indexOf(Object o) 

Parâmetros

O parâmetro 'o' representa o elemento a ser pesquisado.

Lança:

ClassCastException - Se o tipo do elemento especificado não for compatível com esta lista.

Null Pointer Exception - Se o elemento especificado for nulo e esta lista não permitir elementos nulos.

Retornar

O método indexOf() retorna o índice da primeira ocorrência do elemento especificado se estiver presente nesta lista, caso contrário, retorna -1.

Exemplo 1

 import java.util.LinkedList; import java.util.List; public class JavaListIndexOfExample1 { public static void main(String[] args) { List list= new LinkedList(); for (int i=0;i<6;i++){ list.add(i); returns the element at specified position in this list int value="list.indexOf(i);" system.out.println('element stored index '+i+' : '+value); } < pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Element stored at index 0 : 0 Element stored at index 1 : 1 Element stored at index 2 : 2 Element stored at index 3 : 3 Element stored at index 4 : 4 Element stored at index 5 : 5 </pre> <h2>Example 2</h2> <pre> import java.util.LinkedList; import java.util.List; public class JavaListIndexOfExample2 { public static void main(String[] args) { List list= new LinkedList(); list.add(null); list.add(null); list.add(null); // returns -1 if the no value is present in the specified index int value =list.indexOf(90); System.out.println(&apos;Element stored at Index &apos;+90+&apos; : &apos;+value); } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Element stored at Index 90 : -1 </pre> <h2>Example 3</h2> <pre> import java.util.LinkedList; import java.util.List; public class JavaListIndexOfExample3 { public static void main(String[] args) { List list= new LinkedList(); list.add(67); list.add(89); // returns -1 if the no value is present in the specified index int value =list.indexOf(null); System.out.println(&apos;Element stored at &apos;+null+&apos; : &apos;+value); } } </pre> <span> Test it Now </span> <p> <strong>Output:</strong> </p> <pre> Element stored at null : -1 </pre></6;i++){>

Exemplo 2

 import java.util.LinkedList; import java.util.List; public class JavaListIndexOfExample2 { public static void main(String[] args) { List list= new LinkedList(); list.add(null); list.add(null); list.add(null); // returns -1 if the no value is present in the specified index int value =list.indexOf(90); System.out.println(&apos;Element stored at Index &apos;+90+&apos; : &apos;+value); } } 
Teste agora

Saída:

 Element stored at Index 90 : -1 

Exemplo 3

 import java.util.LinkedList; import java.util.List; public class JavaListIndexOfExample3 { public static void main(String[] args) { List list= new LinkedList(); list.add(67); list.add(89); // returns -1 if the no value is present in the specified index int value =list.indexOf(null); System.out.println(&apos;Element stored at &apos;+null+&apos; : &apos;+value); } } 
Teste agora

Saída:

 Element stored at null : -1