JavaTuple lastIndexOf()方法

这个 lastIndexOf() 方法 组织。Java元组 用于在元组类中查找作为参数传递的值的最后一个索引。此方法接受对象类型的参数,以便可以检查所有类型的值。它继承自JavaTuple类。该方法可用于javatuples库的任何元组类对象。如果多次找到,它将返回作为参数传递的值的最后一个索引int。如果只找到一次,它将返回该索引。如果没有找到,则返回-1。

null

方法声明:

public final int lastIndexOf(Object value)

语法:

int index = TupleClassObject.lastIndexOf(Object value)

在这里 TupleClassObject 表示使用的JavaTuple类对象,如Unit、Quintet、Deced等。

返回值: 如果多次找到,此方法返回一个int,它是作为参数传递的值的最后一个索引。如果只找到一次,它将返回该索引。如果没有找到,则返回-1。

下面的程序将演示使用lastIndexOf()方法的各种方法:

项目1: 将lastIndexOf()与Unit类一起使用:

// Below is a Java program to use lastIndexOf() method
import java.util.*;
import org.javatuples.Unit;
class GfG {
public static void main(String[] args)
{
// Creating an Unit with one value
Unit<String> unit = Unit.with( "GeeksforGeeks" );
// Using lastIndexOf() method for present value
int index = unit.lastIndexOf( "GeeksforGeeks" );
// Printing the index
System.out.println( "Last Index of GeeksforGeeks = "
+ index);
// Using lastIndexOf() method for absent value
index = unit.lastIndexOf( "Present" );
// Printing the index
System.out.println( "Last Index of Present = "
+ index);
}
}


输出:

Last Index of GeeksforGeeks = 0
Last Index of Present = -1

项目2: 将lastIndexOf()与Quarter类一起使用:

// Below is a Java program to use lastIndexOf() method
import java.util.*;
import org.javatuples.Quartet;
class GfG {
public static void main(String[] args)
{
// Creating a quartet
Quartet<Integer, Integer, Integer, String> quartet
= Quartet.with(Integer.valueOf( 1 ),
Integer.valueOf( 1 ),
Integer.valueOf( 1 ),
"GeeksforGeeks" );
// Using lastIndexOf() method for value
// present more than once
int index = quartet.lastIndexOf( 1 );
// Printing the index
System.out.println( "Last Index of 1 = "
+ index);
// Using lastIndexOf() method for value
// present just once
index = quartet.lastIndexOf( "GeeksforGeeks" );
// Printing the index
System.out.println( "Last Index of GeeksforGeeks = "
+ index);
// Using lastIndexOf() method for value
// not present
index = quartet.lastIndexOf( 20.5 );
// Printing the index
System.out.println( "Last Index of 20.5 = "
+ index);
}
}


输出:

Last Index of 1 = 2
Last Index of GeeksforGeeks = 3
Last Index of 20.5 = -1

注: 类似地,它可以与任何其他JavaTuple类一起使用。

© 版权声明
THE END
喜欢就支持一下吧
点赞12 分享