logo

Llista Java indexOf() Mètode

El mètode indexOf() de la interfície List retorna l'índex de la primera ocurrència de l'element especificat en aquesta llista. Retorna -1 si l'element especificat no està present en aquesta llista.

Sintaxi

 public int indexOf(Object o) 

Paràmetres

El paràmetre 'o' representa l'element a cercar.

Llançaments:

ClassCastException - Si el tipus de l'element especificat no és compatible amb aquesta llista.

NullPointerException - Si l'element especificat és nul i aquesta llista no permet elements nuls.

Tornar

El mètode indexOf() retorna l'índex de la primera ocurrència de l'element especificat si està present en aquesta llista, en cas contrari retorna -1.

Exemple 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++){>

Exemple 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); } } 
Prova-ho ara

Sortida:

 Element stored at Index 90 : -1 

Exemple 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); } } 
Prova-ho ara

Sortida:

 Element stored at null : -1