将两个String类型的数组进行合并
1、(1)第一种方法-遍历数组,代码如下:
public static void main(String[] args) {
String[] str1 = {"111","222","333"};
String[] str2 = {"444","555","666"};
String[] newStr = new String[str1.length+str2.length]
for(int x=0;x<str1.length;x++){
newStr[x] = str1[x];
}
for(int y=0;y<str2.length;y++){
newStr[str1.length+y]=str2[y];
}
for(int y=0;y<newStr.length;y++){
System.out.println(newStr[y] + " ");
}
}

2、(2)第二种方法-数组扩容,代码如下:
public static void main(String[] args) {
String[] str1 = {"111","222","333"};
String[] str2 = {"444","555","666"};
int str1Length = str1.length;
int str2length = str2.length;
str1 = Arrays.copyOf(str1, str1Length+str2length);//对数组扩容
System.arraycopy(str2, 0, str1, str1Length, str2length);
System.out.println(Arrays.toString(str1));
}

声明:本网站引用、摘录或转载内容仅供网站访问者交流或参考,不代表本站立场,如存在版权或非法内容,请联系站长删除,联系邮箱:site.kefu@qq.com。
阅读量:21
阅读量:118
阅读量:191
阅读量:165
阅读量:167