在 Java Pair 類元組中獲取值


使用 getValueX() 方法可在特定索引處從 JavaTuples 類中獲取值。例如,getValue0()。

讓我們首先了解使用 JavaTuples 需要什麼。要在 JavaTuples 中使用 Pair 類,你需要匯入以下包 -

import org.javatuples.Pair;

注意 - 下載並執行 JavaTuples 程式 的步驟。如果你使用 Eclipse IDE 在 JavaTuples 中執行 Pair 類,則右鍵單擊 Project → 屬性 → Java 構建路徑 → 新增外部 Jar,然後上傳下載的 JavaTuples jar 檔案。

以下是一個示例 -

示例

import org.javatuples.Pair;
public class Demo {
   public static void main(String[] args) {
      Pair < String, String > p1 = Pair.with("One", "Two");
      Pair < String, String > p2 = p1.setAt1("Three");
      System.out.println("Result = " + p2);
      System.out.println("Fetch Value: " + p2.getValue0());
   }
}

輸出

Result = [One, Three]
Fetch Value: One

更新於: 2019-07-30

365 次瀏覽

開啟你的 職業生涯

透過結業來獲得認證

開始
廣告
© . All rights reserved.