📜  Java中的 AbstractList indexOf() 方法及示例

📅  最后修改于: 2022-05-13 01:55:11.128000             🧑  作者: Mango

Java中的 AbstractList indexOf() 方法及示例

Java.util.AbstractList类的indexOf()方法用于返回此列表中指定元素第一次出现的索引,如果此列表不包含该元素,则返回 -1。更正式地说,返回满足 (o==null ? get(i)==null : o.equals(get(i))) 的最低索引 i,如果没有这样的索引,则返回 -1。

句法:

public int indexOf(Object o)

参数:此方法以Object o作为参数,该参数是要搜索的元素。

返回值:此方法返回此列表中指定元素第一次出现的索引,如果此列表不包含该元素,则返回 -1。

下面是说明indexOf()方法的示例。

示例 1:

// Java program to demonstrate
// indexOf() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList
            AbstractList
                arrlist1 = new ArrayList();
  
            // Populating arrlist1
            arrlist1.add(10);
            arrlist1.add(20);
            arrlist1.add(30);
            arrlist1.add(40);
            arrlist1.add(50);
  
            // print arrlist1
            System.out.println("ArrayListlist : "
                               + arrlist1);
  
            // getting the index of element 30
            // using indexOf() method
            int index = arrlist1.indexOf(30);
  
            // print the index
            System.out.println("index : " + index);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
ArrayListlist : [10, 20, 30, 40, 50]
index : 2

示例 2:

// Java program to demonstrate
// indexOf() method
// for Integer value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv)
        throws Exception
    {
  
        try {
  
            // Creating object of AbstractList
            AbstractList
                arrlist1 = new ArrayList();
  
            // Populating arrlist1
            arrlist1.add(10);
            arrlist1.add(20);
            arrlist1.add(30);
            arrlist1.add(40);
            arrlist1.add(50);
  
            // print arrlist1
            System.out.println("ArrayListlist : "
                               + arrlist1);
  
            // getting the index of element 25
            // using indexOf() method
            int index = arrlist1.indexOf(25);
  
            // print the index
            System.out.println("Index of 25 : "
                               + index);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
ArrayListlist : [10, 20, 30, 40, 50]
Index of 25 : -1

示例 3:对于 Null 值

// Java program to demonstrate
// indexOf() method
// for null value
  
import java.util.*;
  
public class GFG1 {
    public static void main(String[] argv) throws Exception
    {
        try {
  
            // Creating object of AbstractList
            AbstractList
                arrlist1 = new ArrayList();
  
            // Populating arrlist1
            arrlist1.add(10);
            arrlist1.add(20);
            arrlist1.add(30);
            arrlist1.add(40);
            arrlist1.add(50);
  
            // print arrlist1
            System.out.println("ArrayListlist : "
                               + arrlist1);
  
            // creating null object
            Object value = null;
  
            // getting the index of element null
            // using indexOf() method
            int index = arrlist1.indexOf(value);
  
            // print the index
            System.out.println("Index of null : "
                               + index);
        }
  
        catch (NullPointerException e) {
            System.out.println("Exception thrown : " + e);
        }
    }
}
输出:
ArrayListlist : [10, 20, 30, 40, 50]
Index of null : -1