如何在 Java 中建立元祖十元組?


使用建構函式或 with() 方法在 Java 中建立元祖十元組。這裡,我們將看到如何使用建構函式建立元祖十元組。

我們首先來看一下使用 JavaTuples 所需的內容。要使用 JavaTuples 中的 Decade 類,需要匯入以下包 −

import org.javatuples.Decade;

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

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

以下是在 Java 中建立元祖十元組的示例 −

示例

import org.javatuples.Decade;
public class Demo {
   public static void main(String[] args) {
      // Tuple with 10 elements
      Decade<String, String, String, String, String, String, String, String, String, String> d =
         new Decade<String, String, String, String, String, String, String, String, String,
         String> ("AB","CD", "EF","GH", "IJ","KL", "MN","OP", "QR", "ST");
      System.out.println("Elements in the Decade Tuple = ");
      for (Object ele : d)
         System.out.println(ele);
   }
}

輸出

Elements in the Decade Tuple =
AB
CD
EF
GH
IJ
KL
MN
OP
QR
ST

更新於:30-Jul-2019

68 次瀏覽

開啟您的職業生涯

完成該課程獲得認證

開始入門
廣告
© . All rights reserved.