當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


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。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。