Java中如何把两个数组合并为一个
答案:2 悬赏:20 手机版
解决时间 2021-11-30 20:35
- 提问者网友:niaiwoma
- 2021-11-30 03:47
Java中如何把两个数组合并为一个
最佳答案
- 五星知识达人网友:轮獄道
- 2021-11-30 04:16
import java.util.Arrays;
//Java中如何把两个数组合并为一个
public class gog {
public static void main(String[] args) {
String [] str1 = {"J","a","v","a","中"};
String [] str2 = {"如","何","把","两","个","数","组","合","并","为","一","个"};
int strLen1=str1.length;//保存第一个数组长度
int strLen2=str2.length;//保存第二个数组长度
str1= Arrays.copyOf(str1,strLen1+ strLen2);//扩容
System.arraycopy(str2, 0, str1, strLen1,strLen2 );//将第二个数组与第一个数组合并
System.out.println(Arrays.toString(str1));//输出数组
}
}
//Java中如何把两个数组合并为一个
public class gog {
public static void main(String[] args) {
String [] str1 = {"J","a","v","a","中"};
String [] str2 = {"如","何","把","两","个","数","组","合","并","为","一","个"};
int strLen1=str1.length;//保存第一个数组长度
int strLen2=str2.length;//保存第二个数组长度
str1= Arrays.copyOf(str1,strLen1+ strLen2);//扩容
System.arraycopy(str2, 0, str1, strLen1,strLen2 );//将第二个数组与第一个数组合并
System.out.println(Arrays.toString(str1));//输出数组
}
}
全部回答
- 1楼网友:我住北渡口
- 2021-11-30 05:14
static Object[] merge(Object[] a1, Object[] a2) {
Object[] a3 = new Object[a1.length + a2.length];
System.arraycopy(a1, 0, a3, 0, a1.length);
System.arraycopy(a2, 0, a3, a1.length, a2.length);
return a3;
}
Object[] a3 = new Object[a1.length + a2.length];
System.arraycopy(a1, 0, a3, 0, a1.length);
System.arraycopy(a2, 0, a3, a1.length, a2.length);
return a3;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯