lastindexof方法_Java Vector lastIndexOf()方法与示例

本文详细介绍了 Java 中 Vector 类的 lastIndexOf 方法的用法。该方法用于查找并返回给定对象最后一次出现的索引,包括从特定索引开始的搜索。文章通过实例展示了如何使用此方法,并解释了可能抛出的异常。

lastindexof方法

向量类别的lastIndexOf()方法 (Vector Class lastIndexOf() method)

Syntax:

句法:

    public int lastIndexOf (Object ob);
    public int lastIndexOf (Object ob, int indices);

  • lastIndexOf() method is available in java.util package.

    lastIndexOf()方法在java.util包中可用。

  • lastIndexOf(Object ob) method is used to return the index of the last occurrence of the given element.

    lastIndexOf(Object ob)方法用于返回给定元素的最后一次出现的索引。

  • lastIndexOf(Object ob, int indices) method is used to find the index of the last occurrence of the given object in this Vector and searching starts at the given indices.

    lastIndexOf(Object ob,int index)方法用于查找此Vector中给定对象最后一次出现的索引,并从给定索引开始搜索。

  • These methods may throw an exception at the time of returning an index.

    这些方法在返回索引时可能会引发异常。

    IndexOutOfBoundsException: This exception may throw when the given parameter is not in a range.

    IndexOutOfBoundsException :如果给定参数不在范围内,则可能引发此异常。

  • These are non-static methods and it is accessible with class objects and if we try to access these methods with the class name then we will get an error.

    这些是非静态方法,可通过类对象访问,如果尝试使用类名访问这些方法,则会收到错误消息。

Parameter(s):

参数:

  • In the first case, lastIndexOf(Object ob)

    在第一种情况下, lastIndexOf(Object ob)

    • Object ob – represents the object for which the last occurrence element index to be returned.
    • 对象ob –代表要为其返回最后一个出现元素索引的对象。
  • In the first case, lastIndexOf (Object ob, int indices)

    在第一种情况下, lastIndexOf(对象ob,int索引)

    • Object ob – represents the object for which the last occurrence element index to be returned.
    • 对象ob –代表要为其返回最后一个出现元素索引的对象。
    • int indices – represents the index of searching starts.
    • int index –表示搜索开始的索引。

Return value:

返回值:

In both the cases, the return type of the method is int - it gets the index of the last occurrence of the given object when exists otherwise it returns -1.

在这两种情况下,方法的返回类型均为int-它在存在时获取给定对象最后一次出现的索引,否则返回-1。

Example:

例:

// Java program to demonstrate the example 
// of lastIndexOf() method of Vector

import java.util.*;

public class LastIndexOfVector {
 public static void main(String[] args) {
  // Instantiates a vector object     
  Vector < String > v = new Vector < String > (10);

  // By using add() method is to add
  // the elements in vector
  v.add("C");
  v.add("C++");
  v.add("SFDC");
  v.add("C++");
  v.add("JAVA");

  //Display Vector
  System.out.println("v: " + v);

  // By using lastIndexOf(object) method is used
  // to return the index of last occurrence of the
  // given object
  System.out.println("v.lastIndexOf(C++): " + v.lastIndexOf("C++"));

  // By using lastIndexOf(object, indices) method is used
  // to return the index of last occurrence of the
  // given object and searching starts from the 
  // given indices
  System.out.println("v.lastIndexOf(C++,4): " + v.lastIndexOf("C++", 4));
 }
}

Output

输出量

v: [C, C++, SFDC, C++, JAVA]
v.lastIndexOf(C++): 3
v.lastIndexOf(C++,4): 3


翻译自: https://www.includehelp.com/java/vector-lastindexof-method-with-example.aspx

lastindexof方法

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值