Java 元組 setAt1() Pair 類方法
setAt0() 方法用於在 JavaTuples 中設定 Pair 值,並返回一個副本,其中指定索引(此處為索引 1)位置處的新值為 val。
首先讓我們瞭解使用 JavaTuples 需要做什麼。若要在 JavaTuples 中使用 Pair 類,你需要匯入以下包 −
import org.javatuples.Pair;
注意 − 下載和執行 JavaTuples 程式的步驟:如果你使用 Eclipse IDE 在 JavaTuples 中執行 Pair 類,則右鍵單擊 專案 → 屬性 → 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); } }
輸出
Result = [One, Three]
廣告