数组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对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序