📜  使用 JavaTuples 在Java中使用 Triplet Class 实现 Quartet Class

📅  最后修改于: 2022-05-13 01:55:46.637000             🧑  作者: Mango

使用 JavaTuples 在Java中使用 Triplet Class 实现 Quartet Class

以下是使用 Triplet Class 实现 Quartet Class 的方法

  1. 使用直接值
    import java.util.*;
    import org.javatuples.*;
      
    class GfG {
        public static void main(String[] args)
        {
      
            // create Triplet
            Triplet
                triplet = new Triplet(
                    "Triplet 1", "1", "GeeksforGeeks");
      
            // Print Triplet
            System.out.println("Triplet: " + triplet);
      
            // Create Quartet from Triplet
            Quartet
                quartet = new Quartet(
                    "Quartet 1",
                    triplet.getValue0(),
                    triplet.getValue1(),
                    triplet.getValue2());
      
            // Print Quartet
            System.out.println("Quartet: " + quartet);
        }
    }
    

    输出:

    [Triplet 1, 1, GeeksforGeeks]
    [Quartet 1, Triplet 1, 1, GeeksforGeeks]
  2. 使用 Triplet.add() 方法
    import java.util.*;
    import org.javatuples.*;
      
    class GfG {
        public static void main(String[] args)
        {
            // create Triplet
            Triplet
                triplet = new Triplet(
                    "Triplet 1", "1", "GeeksforGeeks");
      
            // Print Triplet
            System.out.println("Triplet: " + triplet);
      
            // Using add() to create Quartet
            Quartet
                quartet = triplet.add("Quartet 1");
      
            // Print Quartet
            System.out.println("Quartet: " + quartet);
        }
    }
    

    输出:

    Triplet: [Triplet 1, 1, GeeksforGeeks]
    Quartet: [Triplet 1, 1, GeeksforGeeks, Quartet 1]
  3. 使用 Triplet.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 Triplet
            Triplet
                triplet = new Triplet(
                    "Triplet 1", "1", "GeeksforGeeks");
      
            // Print Triplet
            System.out.println("Triplet: " + triplet);
      
            // Using add() to create Quartet
            Quartet
                quartet = triplet.addAt0("Quartet 1");
      
            // Print Quartet
            System.out.println("Quartet: " + quartet);
        }
    }
    

    输出:

    Triplet: [Triplet 1, 1, GeeksforGeeks]
    Quartet: [Quartet 1, Triplet 1, 1, GeeksforGeeks]

    程序 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 Triplet
            Triplet
                triplet = new Triplet(
                    "Triplet 1", "1", "GeeksforGeeks");
      
            // Print Triplet
            System.out.println("Triplet: " + triplet);
      
            // Using add() to create Quartet
            Quartet
                quartet = triplet.addAt1("Quartet 1");
      
            // Print Quartet
            System.out.println("Quartet: " + quartet);
        }
    }
    

    输出:

    Triplet: [Triplet 1, 1, GeeksforGeeks]
    Quartet: [Triplet 1, Quartet 1, 1, GeeksforGeeks]

    程序 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 Triplet
            Triplet
                triplet = new Triplet(
                    "Triplet 1", "1", "GeeksforGeeks");
      
            // Print Triplet
            System.out.println("Triplet: " + triplet);
      
            // Using add() to create Quartet
            Quartet
                quartet = triplet.addAt2("Quartet 1");
      
            // Print Quartet
            System.out.println("Quartet: " + quartet);
        }
    }
    

    输出:

    Triplet: [Triplet 1, 1, GeeksforGeeks]
    Quartet: [Triplet 1, 1, Quartet 1, GeeksforGeeks]

    程序 4:使用 addAt3() 在位置 3 添加

    // 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 Triplet
            Triplet
                triplet = new Triplet(
                    "Triplet 1", "1", "GeeksforGeeks");
      
            // Print Triplet
            System.out.println("Triplet: " + triplet);
      
            // Using add() to create Quartet
            Quartet
                quartet = triplet.addAt3("Quartet 1");
      
            // Print Quartet
            System.out.println("Quartet: " + quartet);
        }
    }
    

    输出:

    Triplet: [Triplet 1, 1, GeeksforGeeks]
    Quartet: [Triplet 1, 1, GeeksforGeeks, Quartet 1]