當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Java Guava Longs.concat()用法及代碼示例


Guava庫中Longs類的concat()方法用於將許多數組的值連接到一個數組中。將這些要連接的長數組指定為此方法的參數。

For example: concat(new long[] {1, 2}, new long[] {3, 4, 5}, new long[] {6, 7}
returns the array {1, 2, 3, 4, 5, 6, 7}.

用法:


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

參數:此方法接受數組作為參數,這是此方法要連接的長數組。

返回值:此方法返回單個long數組,該數組按原始順序包含所有指定的long數組值。

以下示例程序旨在說明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…-



相關用法


注:本文由純淨天空篩選整理自Sahil_Bansall大神的英文原創作品 Java Guava | Longs.concat() method with Examples。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。