入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

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

创建时间:2016-08-03 投稿人: 浏览次数:1984
<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>

声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。