从 Java 中的八元组获取值
若要从八元组获取值,请使用方法 getValueX()。其中的 X 是你想要获取的值的索引。
例如,若要获取第 5 个元素,即第 4 个索引,请按下列方式使用 getValueX() 方法:
getValue(4);
首先,让我们看看使用 JavaTuples 需要做哪些工作。要在 JavaTuples 中使用八元组类,你需要导入以下程序包:
import org.javatuples.Octet;
注意 - 下载 JavaTuples Jar 库以运行 JavaTuples 程序。如果你使用的是 Eclipse IDE,请右键单击 项目 -》属性 -》Java 构建路径 -》添加外部 Jar,然后上传下载的 JavaTuples jar 文件。在下文中,你可以查阅运行 JavaTuples 的所有步骤:
步骤 - 在 Eclipse 中运行 JavaTuples 程序
以下为从 Java 中的八元组获取值的示例 -
示例
import org.javatuples.Octet;
import java.util.*;
public class Demo {
public static void main(String[] args) {
String[] strArr = {"laptop", "desktop","mobile", "tablet","monitor", "LCD","LED", "OLED"};
Octet<String, String, String, String, String, String, String, String> oc = Octet.fromArray(strArr);
System.out.println("Result = " + oc);
// index 0
System.out.println("Value 1 = "+oc.getValue0());
// index 1
System.out.println("Value 2 = "+oc.getValue1());
// index 2
System.out.println("Value 3 = "+oc.getValue2());
// index 3
System.out.println("Value 4 = "+oc.getValue3());
}
}输出
Result = [laptop, desktop, mobile, tablet, monitor, LCD, LED, OLED] Value 1 = laptop Value 2 = desktop Value 3 = mobile Value 4 = tablet
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP