网站首页 美食营养 游戏数码 手工爱好 生活家居 健康养生 运动户外 职场理财 情感交际 母婴教育 时尚美容

将两个String类型的数组进行合并

时间:2024-10-12 18:39:16

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] + " "); }}

将两个String类型的数组进行合并

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));}

将两个String类型的数组进行合并
© 2025 智德知识库
信息来自网络 所有数据仅供参考
有疑问请联系站长 site.kefu@gmail.com