從 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

更新時間:2019-07-30

82 次瀏覽

開啟你的 職業生涯

透過完成課程獲得認證

立即開始
廣告