📜  JavaTuples 中的 Ennead 类

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

JavaTuples 中的 Ennead 类

EnneadJavaTuples库中处理 9 个元素的元组。由于这个 Ennead 是一个泛型类,它可以在其中保存任何类型的值。

由于 Ennead 是一个元组,因此它也具有 JavaTuples 的所有特性:

  • 它们是类型安全
  • 它们是不可变
  • 它们是可迭代
  • 它们是可序列化
  • 它们是可比较的(实现 Comparable
  • 他们实现了 equals()hashCode()
  • 他们还实现了 toString()

类声明

public final class Ennead
extends Tuple
implements IValue0, IValue2, IValue3, IValue4, 
           IValue5 

类层次结构

Object
  ↳ org.javatuples.Tuple
      ↳ org.javatuples.Ennead 

创建 Ennead 元组

  • 从构造函数
    语法
Ennead ennead = 
    new Ennead
        (value1, value2, value3, value4, value5, value6, value7, value8, value9);
  • 示例
Java
// Below is a Java program to create
// a Ennead tuple from Constructor
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        System.out.println(ennead);
    }
}


Java
// Below is a Java program to create
// a Ennead tuple from with() method
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        System.out.println(ennead);
    }
}


Java
// Below is a Java program to create
// a Ennead tuple from Collection
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        // Creating Ennead from List
        List list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
 
        Ennead ennead
            = Ennead.fromCollection(list);
 
        // Creating Ennead from Array
        Integer[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
        Ennead otherEnnead
            = Ennead.fromArray(arr);
 
        System.out.println(ennead);
        System.out.println(otherEnnead);
    }
}


Java
// Below is a Java program to get
// a Ennead value
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        System.out.println(ennead.getValue0());
        System.out.println(ennead.getValue2());
    }
}


Java
// Below is a Java program to set
// a Ennead value
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        Ennead otherEnnead
            = ennead.setAt3(40);
 
        System.out.println(otherEnnead);
    }
}


Java
// Below is a Java program to add
// a value
 
import java.util.*;
import org.javatuples.Ennead;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        Decade ennead
            = ennead.addAt9(10);
 
        System.out.println(ennead);
    }
}


Java
// Below is a Java program to search
// a value in a Ennead
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        boolean exist = ennead.contains(5);
        boolean exist1 = ennead.contains(false);
 
        System.out.println(exist);
        System.out.println(exist1);
    }
}


Java
// Below is a Java program to iterate
// a Ennead
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        for (Object item : ennead)
            System.out.println(item);
    }
}


输出:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
  • 使用 with() 方法:with() 方法是 JavaTuples 库提供的一个函数,用于实例化具有此类值的对象。
    语法
Ennead ennead = 
    Ennead.with(value1, value2, value3, value4, value5, value6, value7, value8, value9);
  • 示例

Java

// Below is a Java program to create
// a Ennead tuple from with() method
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        System.out.println(ennead);
    }
}

输出:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
  • 从其他集合: fromCollection() 方法用于从集合创建元组,fromArray() 方法用于从数组创建。集合/数组必须与元组具有相同的类型,并且集合/数组中的值的数量必须与元组类匹配。
    语法
Ennead ennead = 
    Ennead.fromCollection(collectionWith_9_value);

Ennead ennead = 
    Ennead.fromArray(arrayWith_9_value);
  • 示例

Java

// Below is a Java program to create
// a Ennead tuple from Collection
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        // Creating Ennead from List
        List list = new ArrayList();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        list.add(6);
        list.add(7);
        list.add(8);
        list.add(9);
 
        Ennead ennead
            = Ennead.fromCollection(list);
 
        // Creating Ennead from Array
        Integer[] arr = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
 
        Ennead otherEnnead
            = Ennead.fromArray(arr);
 
        System.out.println(ennead);
        System.out.println(otherEnnead);
    }
}

输出:

[1, 2, 3, 4, 5, 6, 7, 8, 9]
[1, 2, 3, 4, 5, 6, 7, 8, 9]

获得价值

getValueX() 方法可用于获取元组中索引 X 处的值。元组中的索引从 0 开始。因此索引 X 处的值表示位置 X+1 处的值。

语法

Ennead ennead = 
    new Ennead
        (value1, value2, value3, value4, value5, value6, value7, value8, value9);

type1 val1 = ennead.getValue0();

示例

Java

// Below is a Java program to get
// a Ennead value
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        System.out.println(ennead.getValue0());
        System.out.println(ennead.getValue2());
    }
}

输出:

1
3

设置 Ennead 值

由于元组是不可变的,这意味着不可能修改任何索引处的值。
因此,JavaTuples 提供了 setAtX(value) ,它在索引 X 处创建具有新值的元组副本,并返回该元组。

句法:

Ennead ennead = 
    new Ennead
                (value1, value2, value3, value4, value5, value6, value7, value8, value9);

Ennead 
    otherEnnead = ennead.setAtX(value);

例子:

Java

// Below is a Java program to set
// a Ennead value
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        Ennead otherEnnead
            = ennead.setAt3(40);
 
        System.out.println(otherEnnead);
    }
}

输出:

[1, 2, 3, 40, 5, 6, 7, 8, 9] 

添加一个值

添加值可以在addAtX()方法的帮助下完成,其中 X 表示要添加值的索引。此方法返回比被调用元组多一个元素的元组。

语法

Ennead ennead = 
    new Ennead
        (value1, value2, value3, value4, value5, value6, value7, value8, value9);

Ennead ennead = 
    ennead.addAtx(value);

示例

Java

// Below is a Java program to add
// a value
 
import java.util.*;
import org.javatuples.Ennead;
import org.javatuples.Decade;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        Decade ennead
            = ennead.addAt9(10);
 
        System.out.println(ennead);
    }
}

输出:

[1, 2, 3, 4, 5, 6, 7, 8, 9, 10] 

在 Ennead 中搜索

可以使用预定义的方法contains()在元组中搜索元素。无论该值是否存在,它都会返回一个布尔值。

语法

Ennead ennead = 
    new Ennead
        (value1, value2, value3, value4, value5, value6, value7, value8, value9);

boolean res = ennead.contains(value2);

示例

Java

// Below is a Java program to search
// a value in a Ennead
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        boolean exist = ennead.contains(5);
        boolean exist1 = ennead.contains(false);
 
        System.out.println(exist);
        System.out.println(exist1);
    }
}

输出:

true
false

遍历 Ennead

由于 Ennead 实现了Iterable接口。这意味着它们可以像集合或数组一样被迭代。

语法

Ennead ennead = 
    new Ennead
            (value1, value2, value3, value4, value5, value6, value7, value8, value9);

for (Object item : ennead) {
        ...
}

示例

Java

// Below is a Java program to iterate
// a Ennead
 
import java.util.*;
import org.javatuples.Ennead;
 
class GfG {
    public static void main(String[] args)
    {
        Ennead ennead
            = Ennead.with(Integer.valueOf(1),
                          Integer.valueOf(2),
                          Integer.valueOf(3),
                          Integer.valueOf(4),
                          Integer.valueOf(5),
                          Integer.valueOf(6),
                          Integer.valueOf(7),
                          Integer.valueOf(8),
                          Integer.valueOf(9));
 
        for (Object item : ennead)
            System.out.println(item);
    }
}

输出:

1
2
3
4
5
6
7
8
9