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


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


Guava 的Ints.concat()方法用於將作為參數傳遞的數組組合到單個數組中。此方法從每個提供的數組返回的值組合成一個數組。例如,concat(new int [] {a,b},new int [] {},new int [] {c}返回數組{a,b,c}。

用法:

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

參數:此方法將數組作為參數,表示零個或多個int數組。


返回值:此方法按順序返回包含源數組中所有值的單個數組。

範例1:

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

範例2:

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

參考: https://google.github.io/guava/releases/22.0/api/docs/com/google/common/primitives/Ints.html#concat-int:A…-



相關用法


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