在 Java 中对 Pair Tuple 添加值
addAtX() 方法用于添加一个值,在特定位置上显示为 X。例如,addAt0() 在 0 索引位置,addAt1() 在第一个索引位置等。
让我们首先了解在 JavaTuples 中需要做什么。要在 JavaTuples 中使用 Pair 类,需要导入以下包 −
import org.javatuples.Pair;
我们还使用了 Triplet 类,因此导入以下包 −
import org.javatuples.Triplet;
注意 − 下载并运行 JavaTuples 程序的步骤 如果使用 Eclipse IDE 运行 JavaTuples 中的 Pair 类,则右键单击 Project → Properties → Java Build Path → Add External Jars,然后上传下载的 JavaTuples jar 文件。
以下是一个示例 −
示例
import org.javatuples.Pair;
import org.javatuples.Triplet;
public class Demo {
public static void main(String[] args) {
Pair < String, String > p = Pair.with("Mobile", "Desktop");
System.out.println("Initial = " + p);
Triplet < String, String, String > res = p.addAt0("Tablet");
System.out.println("Result = " + res);
}
}输出
Initial = [Mobile, Desktop] Result = [Tablet, Mobile, Desktop]
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP