当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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<Integer>
            AbstractList<Integer>
                arrlist1 = new ArrayList<Integer>();
  
            // 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<Integer>
            AbstractList<Integer>
                arrlist1 = new ArrayList<Integer>();
  
            // 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:对于空值


// 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<Integer>
            AbstractList<Integer>
                arrlist1 = new ArrayList<Integer>();
  
            // 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




相关用法


注:本文由纯净天空筛选整理自RohitPrasad3大神的英文原创作品 AbstractList indexOf() method in Java with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。