在 Java 中从 Decade Tuple 中获取值

java 8programmingobject oriented programming

要从 Java 中的 Decade Tuple 中获取值,请使用 getAtX() 方法。这里,X 表示索引值,例如索引 1 处的 getAt1()。这将返回 Tuple 中索引 1 处的元素。

首先让我们看看使用 JavaTuples 需要什么。要在 JavaTuples 中使用 Decade 类,您需要导入以下包

import org.javatuples.Decade;

注意 下载 JavaTuples Jar 库以运行 JavaTuples 程序。如果您使用的是 Eclipse IDE,请右键单击项目 -> 属性 -> Java 构建路径 -> 添加外部 Jar,然后上传下载的 JavaTuples jar 文件。请参阅以下指南了解运行 JavaTuples 的所有步骤

步骤:如何在 Eclipse 中运行 JavaTuples 程序

以下是从 Java 中的 Decade Tuple 获取值的示例

示例

import org.javatuples.Decade;
public class Demo {
   public static void main(String[] args) {
      String[] strArr = {"AB", "CD", "EF","GH", "IJ", "KL","MN", "OP", "QR","ST" };
         Decade<String, String, String, String, String, String, String, String, String, String> d = Decade.fromArray(strArr);
      System.out.println("Elements in the Decade Tuple = ");
      System.out.println("Element at index 1 = "+d.getValue1());
      System.out.println("Element at index 4 = "+d.getValue4());
      System.out.println("Element at index 7 = "+d.getValue7());
      System.out.println("Element at index 9 = "+d.getValue9());
   }
}

输出

Elements in the Decade Tuple =
Element at index 1 = CD
Element at index 4 = IJ
Element at index 7 = OP
Element at index 9 = ST

相关文章