{
Integer[] m = { 1, 2, 3, 4, 5 }
Integer[] n = { 3, 4, 6 }
Integer[] b = getB(m, n) for (Integer i : b)
{
System.out.println(i)
}
}
private static Integer[] getB(Integer[] m, Integer[] n)
{// 将数组转换为set集合
Set<Integer>set1 = new HashSet<Integer>(Arrays.asList(m))
Set<Integer>set2 = new HashSet<Integer>(Arrays.asList(n)) // 合并两个集合set1.addAll(set2)
Integer[] arr = {} return set1.toArray(arr)
}
三种字符数组合并的方法public static String[] getOneArray() {
String[] a = { "0", "1", "2" }
String[] b = { "0", "1", "2" }
String[] c = new String[a.length + b.length]
for (int j = 0 j < a.length ++j) {
c[j] = a[j]
}
for (int j = 0 j < b.length ++j) {
c[a.length + j] = b[j]
}
return c
}
public static Object[] getTwoArray() {
String[] a = { "0", "1", "2" }
String[] b = { "0", "1", "2" }
List aL = Arrays.asList(a)
List bL = Arrays.asList(b)
List resultList = new ArrayList()
resultList.addAll(aL)
resultList.addAll(bL)
Object[] result = resultList.toArray()
return result
}
public static String[] getThreeArray() {
String[] a = { "0", "1", "2", "3" }
String[] b = { "4", "5", "6", "7", "8" }
String[] c = new String[a.length + b.length]
System.arraycopy(a, 0, c, 0, a.length)
System.arraycopy(b, 0, c, a.length, b.length)
return c
}
Reference: http://www.cnblogs.com/changhongzhi/articles/2242323.html
java里java字符串数组合并成一个数组方法如下:
//方法一 Arrays类
String[] a = {"A","B","C"}
String[] b = {"D","E"}
// List<String>list = Arrays.asList(a) --OK
// List<String>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 = new String[list.size()]
list.toArray(str)
for(int x=0x<str.lengthx++){
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=0x<str1.lengthx++){
newStr[x] = str1[x]
}
for(int y=0y<str2.lengthy++){
newStr[str1.length+y]=str2[y]
}
for(int y=0y<newStr.lengthy++){
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))