当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。