JAVA怎么合并两个数组

Python014

JAVA怎么合并两个数组,第1张

三种字符数组合并的方法

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

int[] arr1 = {1,2,3,4,11}

int[] arr2 = {6,7,8,9,10}

int newLength = arr1.length + arr2.length

int[] arr_target = new int[newLength]

//参数:源数组,源数组起始位置,目标数组,目标数组起始位置,复制长度

System.arraycopy(arr1, 0, arr_target, 0, arr1.length)

System.arraycopy(arr2, 0, arr_target, arr1.length, arr2.length)

//输出合并后数组

for (int i : arr_target) {

System.out.println(i)

}

//排序

Arrays.sort(arr_target)

//输出排序数组

for (int i : arr_target) {

System.out.println(i)

}

//逆序

int[] arr_reverse = new int[newLength]

int flag = 0

for (int i : arr_target) {

arr_reverse[newLength - flag - 1] = i

flag++

}

//输出逆序数组

for (int i : arr_reverse) {

System.out.println(i)

}

数组合并不一定非得遍历

具体的输出题主自己再修改吧