Java番石榴 |带有示例的 Chars.concat() 方法
Guava 库中 Chars 类的concat()方法用于将多个数组的值连接成一个数组。这些要连接的字符数组被指定为此方法的参数。
For example: concat(new char[] {a, b}, new char[] {}, new char[] {c}
returns the array {a, b, c}.
句法:
public static char[] concat(char[]... arrays)
参数:此方法接受数组作为参数,该参数是此方法要连接的字符数组。
返回值:此方法返回一个单独的 char 数组,其中包含所有指定的 char 数组值的原始顺序。
下面的程序说明了 concat() 方法的使用:
示例 1:
// Java code to show implementation of
// Guava's Chars.concat() method
import com.google.common.primitives.Chars;
import java.util.Arrays;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating 2 Character arrays
char[] arr1 = { 'H', 'E', 'L', 'L', 'O' };
char[] arr2 = { 'G', 'E', 'E', 'K', 'S' };
// Using Chars.concat() method to combine
// elements from both arrays into a single array
char[] res = Chars.concat(arr1, arr2);
// Displaying the single combined array
System.out.println(Arrays.toString(res));
}
}
输出:
[H, E, L, L, O, G, E, E, K, S]
示例 2:
// Java code to show implementation of
// Guava's Chars.concat() method
import com.google.common.primitives.Chars;
import java.util.Arrays;
class GFG {
// Driver's code
public static void main(String[] args)
{
// Creating 4 Character arrays
char[] arr1 = { '1', '2', '3' };
char[] arr2 = { '4', '5' };
char[] arr3 = { '6', '7', '8' };
char[] arr4 = { '9', '0' };
// Using Chars.concat() method to combine
// elements from both arrays into a single array
char[] res = Chars.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/18.0/api/docs/com/google/common/primitives/Chars.html#concat(char[]…)