• Tags
  •         
  • www.breakyizhan.com
  •    

    Octet 类是从元组JavaTuples。由于此Octet 是一个泛型类,因此它可以包含任何类型的值。由于Octet 是一个元组,因此它也具有JavaTuples的所有特征:

    • Typesafe
    • 永恒的
    • 可迭代
    • Serializable
    • Comparable(实现Comparable <Tuple>)
    • 实现equals()hashCode()
    • 实现toString()

    Octet 类声明

    public final class Octet<A, B, C, D, E, F, G, H> extends Tuple
    implements IValue0<A>, IValue1<B>, IValue2<C>, IValue3<D>, IValue4<E>, 
                                                IValue5<F, IValue6<G, IValue7<H>

    Octet 类层次结构

    Object
      ↳ org.javatuples.Tuple
          ↳ org.javatuples.Octet<A, B, C, D, E, F, G, H>

    创建Octet元组

    • 来自构造函数语法
      Octet<A, B, C, D, E, F, G, H> octet = 
          new Octet<A, B, C, D, E, F, G, H>
              (value1, value2, value3, value4, value5, value6, value7, value8);

      示例

      // Below is a Java program to create
      // a Octet tuple from Constructor
      
      import java.util.*;
      import org.javatuples.Octet;
      
      class GfG {
          public static void main(String[] args)
          {
              Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> octet
                  = Octet.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));
      
              System.out.println(octet);
          }
      }
      

      输出:

      [1,2,3,4,5,6,7,8]
      
    • 使用with()方法:with()方法是JavaTuples库提供的函数,用于使用这些值实例化对象。语法
      Octet <type1,type2,type3,type4,type5,type6,type7> octet = 
          Octet.with(value1,value2,value3,value4,value5,value6,value7,value8);
      

      示例

      // Below is a Java program to create
      // a Octet tuple from with() method
      
      import java.util.*;
      import org.javatuples.Octet;
      
      class GfG {
          public static void main(String[] args)
          {
              Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> octet
                  = Octet.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));
      
              System.out.println(octet);
          }
      }
      

      输出:

      [1,2,3,4,5,6,7,8]
      
    • 来自其他集合:fromCollection()方法用于从集合创建元组,而fromArray()方法用于从数组创建。集合/数组必须与Tuple具有相同的类型,并且集合/数组中的值的数量必须与Tuple类匹配。语法
      Octet <type1,type2,type3,type4,type5,type6,type7> octet = 
          Octet.fromCollection(collectionWith_8_value);
      
      Octet <type1,type2,type3,type4,type5,type6,type7> octet = 
          Octet.fromArray(arrayWith_8_value);
      

      示例

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

      输出:

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

    获得Octet 的值

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

    语法

    Octet<type1, type2, type3, type4, type5, type6, type7> octet = 
        new Octet<type1, type2, type3, type4, type5, type6, type7>
            (value1, value2, value3, value4, value5, value6, value7, value8);
    
    type1 val1 = octet.getValue0();

    示例

    // Below is a Java program to get
    // a Octet value
    
    import java.util.*;
    import org.javatuples.Octet;
    
    class GfG {
        public static void main(String[] args)
        {
            Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> octet
                = Octet.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));
    
            System.out.println(octet.getValue0());
            System.out.println(octet.getValue2());
        }
    }
    

    输出:

    1
    3
    

    设置Octet 的值

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

    语法

    Octet<type1, type2, type3, type4, type5, type6, type7> octet = 
        new Octet<type1, type2, type3, type4, type5, type6, type7>
          (value1, value2, value3, value4, value5, value6, value7, value8);
    
    Octet<type1, type2, type3, type4, type5, type6, type7> 
        otherOctet = octet.setAtX(value);

    示例

    // Below is a Java program to set
    // a Octet value
    
    import java.util.*;
    import org.javatuples.Octet;
    
    class GfG {
        public static void main(String[] args)
        {
            Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> octet
                = Octet.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));
    
            Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> otherOctet
                = octet.setAt3(40);
    
            System.out.println(otherOctet);
        }
    }
    

    输出:

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

    添加Octet 的值

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

    语法

    Octet<type1, type2, type3, type4, type5, type6, type7> octet = 
        new Octet<type1, type2, type3, type4, type5, type6, type7>
            (value1, value2, value3, value4, value5, value6, value7, value8);
    
    Octet<type 1, type 2, type 3, type 4, type 5, type 6, type 7> octet = 
        octet.addAtx(value);

    示例

    // Below is a Java program to add
    // a value
    
    import java.util.*;
    import org.javatuples.Octet;
    import org.javatuples.Ennead;
    
    class GfG {
        public static void main(String[] args)
        {
            Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> octet
                = Octet.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));
    
            Ennead<Integer, Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> ennead
                = octet.addAt8(9);
    
            System.out.println(ennead);
        }
    }
    

    输出:

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

    在Octet中搜索

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

    语法

    Octet<type1, type2, type3, type4, type5, type6, type7> octet = 
        new Octet<type1, type2, type3, type4, type5, type6, type7>
            (value1, value2, value3, value4, value5, value6, value7, value8);
    
    boolean res = octet.contains(value2);

    示例

    // Below is a Java program to search
    // a value in a Octet
    
    import java.util.*;
    import org.javatuples.Octet;
    
    class GfG {
        public static void main(String[] args)
        {
            Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> octet
                = Octet.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));
    
            boolean exist = octet.contains(5);
            boolean exist1 = octet.contains(false);
    
            System.out.println(exist);
            System.out.println(exist1);
        }
    }
    

    输出:

    true
    false
    

    通过Octet迭代

    因为Octet实现了Iterable <Object>接口。这意味着它们可以以与集合或数组相同的方式进行迭代。

    语法

    Octet<type1, type2, type3, type4, type5, type6, type7> octet = 
        new Octet<type1, type2, type3, type4, type5, type6, type7>
                (value1, value2, value3, value4, value5, value6, value7, value8);
    
    for (Object item : octet) {
            ...
    }

    示例

    // Below is a Java program to iterate
    // a Octet
    
    import java.util.*;
    import org.javatuples.Octet;
    
    class GfG {
        public static void main(String[] args)
        {
            Octet<Integer, Integer, Integer.Integer, Integer, Integer, Integer, Integer> octet
                = Octet.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));
    
            for (Object item : octet)
                System.out.println(item);
        }
    }
    

    输出:

    1
    2
    3
    4
    5
    6
    7
    8

     
    转载请保留页面地址:https://www.breakyizhan.com/java/5430.html