用 Java 迭代 Decade 元組


要迭代 Decade 元組,請像 Java 中的任何其他集合對其進行處理,即使用 for 迴圈,迭代並顯示元素。首先讓我們瞭解在 JavaTuples 中工作的需要。要在 JavaTuples 中處理 Decade 類,您需要匯入以下包

import org.javatuples.Decade;

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

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

以下是用 Java 迭代 Decade 元組的示例

示例

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 =
         Decade.with("AB","CD", "EF","GH", "IJ","KL", "MN","OP", "QR", "ST");
      System.out.println("Iterating the elements in the Decade Tuple = ");
      for (Object ele : d)
         System.out.println(ele);
         boolean res = d.contains("MN");
         System.out.println("Does the value exist in the Decade Tuple? = "+res);
   }
}

輸出

Iterating the elements in the Decade Tuple =
AB
CD
EF
GH
IJ
KL
MN
OP
QR
ST
Does the value exist in the Decade Tuple? = true

更新於: 30-7-2019

136 次瀏覽

開啟你的 職業生涯

完成課程即可獲得認證

開始學習
廣告
© . All rights reserved.