如何在Java中连接两个数组?我需要连接两个String数组在Java中。void f(String[] first, String[] second) {
String[] both = ???}做这件事最简单的方法是什么?
3 回答
12345678_0001
TA贡献1802条经验 获得超5个赞
ArrayUtils.addAll(T[], T...)
String[] both = ArrayUtils.addAll(first, second);
蓝山帝景
TA贡献1843条经验 获得超7个赞
Arrays.copyOf()
ListSystem.arraycopy()
public static <T> T[] concat(T[] first, T[] second) {
T[] result = Arrays.copyOf(first, first.length + second.length);
System.arraycopy(second, 0, result, first.length, second.length);
return result;}public static <T> T[] concatAll(T[] first, T[]... rest) {
int totalLength = first.length;
for (T[] array : rest) {
totalLength += array.length;
}
T[] result = Arrays.copyOf(first, totalLength);
int offset = first.length;
for (T[] array : rest) {
System.arraycopy(array, 0, result, offset, array.length);
offset += array.length;
}
return result;}添加回答
举报
0/150
提交
取消
