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


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


Shorts.concat()是Guava庫中Shorts類的一種方法,用於將多個數組的值連接到一個數組中。例如,concat(new short [] {a,b},new short [] {},new short [] {c}返回數組{a,b,c}。

用法:

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

在這裏,數組代表零個或多個短數組。


返回值:依次包含源數組中所有值的單個數組。

示例1:

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

示例2:

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

參考: https://google.github.io/guava/releases/23.0/api/docs/com/google/common/primitives/Shorts.html#concat-short:A…-




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