Guava – Longs.concat()方法与实例

Guava – Longs.concat()方法及实例

Guava库中Longs类的concat()方法是用来将许多数组的值串联成一个单一的数组。这些要串联的长数组被指定为该方法的参数。

例如:concat(new long[] {1, 2}, new long[] {3, 4, 5}, new long[] {6, 7} 返回数组{1, 2, 3, 4, 5, 6, 7}。

语法:

public static long[] concat(long[]... arrays)

参数: 该方法接受数组作为参数,该数组是要被该方法连接的长数组。

返回值: 该方法返回一个单一的长数组,其中包含所有指定的长数组值的原始顺序。

下面的程序说明了concat()方法的使用。

例1:

// Java code to show implementation of
// Guava's Longs.concat() method
  
import com.google.common.primitives.Longs;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating 2 Long arrays
        long[] arr1 = { 1, 2, 3, 4, 5 };
        long[] arr2 = { 6, 2, 7, 0, 8 };
  
        System.out.println("Long Array 1: "
                           + Arrays.toString(arr1));
  
        System.out.println("Long Array 2: "
                           + Arrays.toString(arr2));
  
        // Using Longs.concat() method to combine
        // elements from both arrays
        // into a single array
        long[] res = Longs.concat(arr1, arr2);
  
        // Displaying the single combined array
        System.out.println("Combined Array: "
                           + Arrays.toString(res));
    }
}

输出:

Long Array 1: [1, 2, 3, 4, 5]
Long Array 2: [6, 2, 7, 0, 8]
Combined Array: [1, 2, 3, 4, 5, 6, 2, 7, 0, 8]

例2:

// Java code to show implementation of
// Guava's Longs.concat() method
  
import com.google.common.primitives.Longs;
import java.util.Arrays;
  
class GFG {
  
    // Driver's code
    public static void main(String[] args)
    {
  
        // Creating 4 long arrays
        long[] arr1 = { 1, 2, 3 };
        long[] arr2 = { 4, 5 };
        long[] arr3 = { 6, 7, 8 };
        long[] arr4 = { 9, 0 };
  
        System.out.println("Long Array 1: "
                           + Arrays.toString(arr1));
  
        System.out.println("Long Array 2: "
                           + Arrays.toString(arr2));
  
        System.out.println("Long Array 3: "
                           + Arrays.toString(arr3));
  
        System.out.println("Long Array 4: "
                           + Arrays.toString(arr4));
  
        // Using Longs.concat() method to combine
        // elements from both arrays
        // into a single array
        long[] res
            = Longs
                  .concat(arr1, arr2, arr3, arr4);
  
        // Displaying the single combined array
        System.out.println("Combined Array: "
                           + Arrays.toString(res));
    }
}

输出:

Long Array 1: [1, 2, 3]
Long Array 2: [4, 5]
Long Array 3: [6, 7, 8]
Long Array 4: [9, 0]
Combined Array: [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

参考资料:

https://google.github.io/guava/releases/21.0/api/docs/com/google/common/primitives/Longs.html#concat-long:A…-

Python教程

Java教程

Web教程

数据库教程

图形图像教程

大数据教程

开发工具教程

计算机教程