您的位置:首页 > 编程语言 > Java开发

数组a,数组b,复制到数组c

2016-08-03 13:04 387 查看
<span style="font-size:18px;">package lianx1;

import java.util.Arrays;

public class ArrayTest2 {
/*
*.现在给出两个数组:    数组A:{1,7,9,11,13,15,17,19};
* · 数组b:{2,4,6,8,10}   两个数组合并为数组c,按升序排列。
*/
public static void main(String[] args) {
int a[] = { 1, 7, 9, 11, 13, 15, 17, 19 };
int b[] = { 2, 4, 6, 8, 10 };
int c[] = new int[a.length + b.length];
copyArray(a, b, c);
copyArray1(a, b, c);
// System.arraycopy(a, 0, c, 0, a.length);
// System.arraycopy(b, 0, c, a.length, b.length);

}
/*
* 通过原生封装好的方法,实现
*/
private static void copyArray1(int[] a, int[] b, int[] c) {
System.arraycopy(a, 0, c, 0, a.length);
System.arraycopy(b, 0, c, a.length, b.length);
Arrays.sort(c);
System.out.println(Arrays.toString(c));

}
/*
* 解法一,通过遍历数组,将数据保存到新的数组
*/
private static void copyArray(int[] a, int[] b, int[] c) {
int index = 0;
for (int i = 0; i < a.length; i++) {
c[index++] = a[i];
}
System.out.println(index);// 8
for (int i = 0; i < b.length; i++) {
c[index++] = b[i];
}
Arrays.sort(c);
System.out.println(Arrays.toString(c));

}

}</span>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java 循环 算法