使用 JavaTuples 在Java中使用 Quartet Class 实现 Quintet Class
以下是使用 Quartet Class 实现 Quintet Class 的方法
- 使用直接值
import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Quartet Quartet
quartet = new Quartet ( "Quartet", "Triplet", "Pair", "Unit"); // Print Quartet System.out.println("Quartet: " + quartet); // Create Quintet from Quartet Quintet quintet = new Quintet ( "Quintet 1", quartet.getValue0(), quartet.getValue1(), quartet.getValue2(), quartet.getValue3()); // Print Quintet System.out.println("Quintet: " + quintet); } } 输出:
[Quintet, Triplet 1, 1, GeeksforGeeks] [Quintet 1, Quintet, Triplet 1, 1, GeeksforGeeks]
- 使用 Quartet.add() 方法
import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Quartet Quartet
quartet = new Quartet ( "Quartet", "Triplet", "Pair", "Unit"); // Print Quartet System.out.println("Quartet: " + quartet); // Create Quintet from Quartet Quintet quintet = quartet.add("Quintet"); // Print Quintet System.out.println("Quintet: " + quintet); } } 输出:
Quartet: [Quartet, Triplet, Pair, Unit] Quintet: [Quartet, Triplet, Pair, Unit, Quintet]
- 使用 Quartet.addAtX() 方法
程序 1:使用 addAt0() 在位置 0 添加
// Below is a Java program to demonstrate // use of addAt0() method with // direct value import java.util.*; import org.javatuples.* class GfG { public static void main(String[] args) { // create Quartet Quartet
quartet = new Quartet ( "Quartet", "Triplet", "Pair", "Unit"); // Print Quartet System.out.println("Quartet: " + quartet); // Create Quintet from Quartet Quintet quintet = quartet.addAt0("Quintet"); // Print Quintet System.out.println("Quintet: " + quintet); } } 输出:
Quartet: [Quartet, Triplet, Pair, Unit] Quintet: [Quintet, Quartet, Triplet, Pair, Unit]
程序 2:使用 addAt1() 在位置 1 添加
// Below is a Java program to demonstrate // use of addAt1() method with // direct value import java.util.*; import org.javatuples.*; class GfG { public static void main(String[] args) { // create Quartet Quartet
quartet = new Quartet ( "Quartet", "Triplet", "Pair", "Unit"); // Print Quartet System.out.println("Quartet: " + quartet); // Create Quintet from Quartet Quintet quintet = quartet.addAt1("Quintet"); // Print Quintet System.out.println("Quintet: " + quintet); } } 输出:
Quartet: [Quartet, Triplet, Pair, Unit] Quintet: [Quartet, Quintet, Triplet, Pair, Unit]
程序 3:使用 addAt2() 在位置 2 添加
// Below is a Java program to demonstrate // use of addAt2() method with // direct value import java.util.*; import org.javatuples.* class GfG { public static void main(String[] args) { // create Quartet Quartet
quartet = new Quartet ( "Quartet", "Triplet", "Pair", "Unit"); // Print Quartet System.out.println("Quartet: " + quartet); // Create Quintet from Quartet Quintet quintet = quartet.addAt2("Quintet"); // Print Quintet System.out.println("Quintet: " + quintet); } } 输出:
Quartet: [Quartet, Triplet, Pair, Unit] Quintet: [Quartet, Triplet, Quintet, Pair, Unit]
程序 4:使用 addAt3() 在位置 3 添加
// Below is a Java program to demonstrate // use of addAt3() method with // direct value import java.util.*; import org.javatuples.* class GfG { public static void main(String[] args) { // create Quartet Quartet
quartet = new Quartet ( "Quartet", "Triplet", "Pair", "Unit"); // Print Quartet System.out.println("Quartet: " + quartet); // Create Quintet from Quartet Quintet quintet = quartet.addAt3("Quintet"); // Print Quintet System.out.println("Quintet: " + quintet); } } 输出:
Quartet: [Quartet, Triplet, Pair, Unit] Quintet: [Quartet, Triplet, Pair, Quintet, Unit]
程序 5:使用 addAt4() 在位置 4 添加
// Below is a Java program to demonstrate // use of addAt4() method with // direct value import java.util.*; import org.javatuples.* class GfG { public static void main(String[] args) { // create Quartet Quartet
quartet = new Quartet ( "Quartet", "Triplet", "Pair", "Unit"); // Print Quartet System.out.println("Quartet: " + quartet); // Create Quintet from Quartet Quintet quintet = quartet.addAt4("Quintet"); // Print Quintet System.out.println("Quintet: " + quintet); } } 输出:
Quartet: [Quartet, Triplet, Pair, Unit] Quintet: [Quartet, Triplet, Pair, Unit, Quintet]