java 两个数组合并的几种方法

网友投稿 259 2023-04-18


java 两个数组合并的几种方法

本文介绍了java 两个数组合并的几种方法,分享给大家,也给自己留个笔记

需求:两个字符串合并(如果想去重复,参考下一篇--数组去重复及记录重复个数)

//方法一 Arrays类

String[] a = {"A","B","C"};

String[] b = {"D","E"};

// List list = Arrays.asList(a); --OK

// List list = Arrays.asList("A","B","C"); --OK

// list.add("F"); --UnsupportedOperationException

// list.remove("A"); --UnsupportedOperationException

// list.set(1,"javaee");--OK (因为是把数组转为集合,其本质还是数组,数组长度固定不变,但内容可以改变)

// 结论:虽然可以把数组转为集合,但是集合长度不能改变

List list = new ArrayList(Arrays.asList(a));

list.addAll(Arrays.asList(b));

String[] str http://= new String[list.size()];

list.toArray(str);

for(int x=0;x

System.out.print(str[x] + " ");

}

//方法二 循环遍历

// 两个数组合并

String[] str1 = {"Hello","world","java"};

String[] str2 = {"Veriable","syntax","interator"};

String[] newStr = new String[str1.length+str2.length];

//newStr = str1;数组是引用类型

for(int x=0;x

newStr[x] = str1[x];

}

for(int y=0;y

newStr[str1.length+y]=str2[y];

}

for(int y=0;y

System.out.println(newStr[y] + " ");

}

// 方法三

String[] str1 = {"Hello","world","java"};

String[] str2 = {"Veriable","syntax","interator"};

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

System.out.print(str[x] + " ");

}

//方法二 循环遍历

// 两个数组合并

String[] str1 = {"Hello","world","java"};

String[] str2 = {"Veriable","syntax","interator"};

String[] newStr = new String[str1.length+str2.length];

//newStr = str1;数组是引用类型

for(int x=0;x

newStr[x] = str1[x];

}

for(int y=0;y

newStr[str1.length+y]=str2[y];

}

for(int y=0;y

System.out.println(newStr[y] + " ");

}

// 方法三

String[] str1 = {"Hello","world","java"};

String[] str2 = {"Veriable","syntax","interator"};

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

newStr[x] = str1[x];

}

for(int y=0;y

newStr[str1.length+y]=str2[y];

}

for(int y=0;y

System.out.println(newStr[y] + " ");

}

// 方法三

String[] str1 = {"Hello","world","java"};

String[] str2 = {"Veriable","syntax","interator"};

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

newStr[str1.length+y]=str2[y];

}

for(int y=0;y

System.out.println(newStr[y] + " ");

}

// 方法三

String[] str1 = {"Hello","world","java"};

String[] str2 = {"Veriable","syntax","interator"};

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

System.out.println(newStr[y] + " ");

}

// 方法三

String[] str1 = {"Hello","world","java"};

String[] str2 = {"Veriable","syntax","interator"};

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:单元测试mock工具比较分析(单元测试mock数据库)
下一篇:微信小程序 获取session_key和openid的实例
相关文章

 发表评论

暂时没有评论,来抢沙发吧~