Javatuples 简明教程
JavaTuples - Add Elements
元组在元组的末尾具有 add() 方法,它也会更改元组的类型。例如,向 Triplet 元组中添加一个元素会将其转换为 Quartet 元组。
A tuple has add() method at the end of a tuple and it changes the type of tuple as well. For example adding a element to Triplet tuple will convert it to a Quartet tuple.
Quartet<String,String,String,String> quartet = triplet.add("Test");
元组还具有 addAtX() 方法,用于从 0 开始在特定索引处添加位置。
A tuple has addAtX() methods as well to add a position at particular index starting from 0.
Quartet<String,String,String,String> quartet = triplet.addAt1("Test");
元组可以使用 addAtX() 方法添加多个元素。
A tuple can add more than one elements using addAtX() methods.
Quartet<String,String,String,String> quartet = pair.addAt1("Test1", "Test2");
元组也可以使用addAtX()方法来添加一个元组。
A tuple can add a tuple as well using addAtX() methods.
Quartet<String,String,String,String> quartet = pair.addAt1(pair1);
Example
让我们来看看 JavaTuples 的实际应用。这里我们将看到如何使用各种方法在元组中添加值。
Let’s see JavaTuples in action. Here we’ll see how to add values in a tuple using various ways.
在 C:>JavaTuples 中创建一个名为 TupleTester 的 Java 类文件。
Create a java class file named TupleTester in C:\>JavaTuples.
文件:TupleTester.java
File: TupleTester.java
package com.tutorialspoint;
import org.javatuples.Pair;
import org.javatuples.Quartet;
import org.javatuples.Quintet;
import org.javatuples.Triplet;
public class TupleTester {
public static void main(String args[]){
Pair<String, Integer> pair = Pair.with("Test", Integer.valueOf(5));
Triplet<String, Integer, String> triplet = pair.add("Test2");
Quartet<String, String, Integer, String> quartet = triplet.addAt1("Test1");
Quintet<String, Integer, String, String, Integer> quintet = triplet.add(pair);
System.out.println("Pair: " + pair);
System.out.println("Triplet:" + triplet);
System.out.println("Quartet:" + quartet);
System.out.println("Quintet:" + quintet);
}
}
Verify the result
Verify the result
使用以下 javac 编译器编译类:
Compile the classes using javac compiler as follows −
C:\JavaTuples>javac -cp javatuples-1.2.jar ./com/tutorialspoint/TupleTester.java
现在运行 TupleTester 查看结果:
Now run the TupleTester to see the result −
C:\JavaTuples>java -cp .;javatuples-1.2.jar com.tutorialspoint.TupleTester