如何在 Java 中建立 LabelValue 元組?


你可以使用 with() 方法或直接使用建構函式來建立一個 LabelValue 元組。我們首先來看看在使用 JavaTuples 之前你需要做什麼。要使用 JavaTuples 中的 LabelValue 類,你需要匯入以下包

import org.javatuples.LabelValue;

注意:下載 JavaTuples Jar 庫來執行 JavaTuples 程式。如果你使用 Eclipse IDE,那麼右鍵單擊專案 -> 屬性 -> Java 構建路徑 -> 新增外部 Jar,然後上傳下載的 JavaTuples jar 檔案。請參閱以下指南,瞭解執行 JavaTuples 的所有步驟

步驟: 如何在 Eclipse 中執行 JavaTuples 程式

以下是建立 LabelValue JavaTuples 類並新增元素的一個示例

示例

import org.javatuples.LabelValue;
public class Demo {
   public static void main(String[] args) {
      LabelValue<Integer, Integer> lbValue = new LabelValue<Integer, Integer>(10, 100);
      System.out.println("Elements in the LabelValue Tuple = "+lbValue);
   }
}

輸出

Elements in the LabelValue Tuple = [10, 100]
Above, we have created the LabelValue tuple with its 2 elements i.e. label and value:
LabelValue<Integer, Integer> lbValue = new LabelValue<Integer, Integer>(10, 100);

更新於: 30-7-2019

74 次瀏覽

開啟你的職業生涯

完成課程獲得認證

開始
廣告