java基础笔记之数组复制与排序
package test;
import java.text.Collator;
import java.util.Arrays;
import java.util.Collections;
public class _03数组的复制与排序 {
public static void main(String[] args) {
/**
数组的复制:
src:
源数组(source)
srcPos:
从源数组开始复制的下标,也就是从哪个开始复制(source position)
dest:
目标数组,复制到哪里去(destination)
destPos:
从指定的下标开始放置复制的元素(destination position)
length:
复制的元素的个数
Arrays.toString(数组名);将数组以指定格式的字符串输出所有元素
*/
int[] src = new int[] {1,2,3};
int[] dest = new int[] {4,5,6,7,8,9};
System.out.println("复制前的src"+Arrays.toString(src));//[1, 2, 3]
System.out.println("复制前的dest"+ Arrays.toString(dest));//[4, 5, 6, 7, 8, 9]
//System.arraycopy(src, 0, dest, 0, src.length);
System.out.println("复制后的src"+Arrays.toString(src));//[1, 2, 3]
//会替换掉之前的值
System.out.println("复制后的dest"+ Arrays.toString(dest));//[1, 2, 3, 7, 8, 9]
//数组合并
//0.创建一个新的数组用来装两个数组的集合,长度为两个数组的长度和
int[] num =new int[src.length+dest.length];
//1.复制src数组
System.arraycopy(src, 0, num, 0, src.length);
System.out.println("第一次粘贴后的num"+Arrays.toString(num));//[1, 2, 3, 0, 0, 0, 0, 0, 0]
//2复制dest数组
System.arraycopy(dest, 0, num, src.length, dest.length);
System.out.println("第二次粘贴后的num"+Arrays.toString(num));//[1, 2, 3, 4, 5, 6, 7, 8, 9]
/**
数组的排序:
Arrays.sort(数组名)
*/
int[] num2 = new int[] {188,15,43,45};
System.out.println("排序前的num2"+Arrays.toString(num2));//[188, 15, 43, 45]
//升序排序
//Arrays.sort(num2);
System.out.println("排序后的num2"+Arrays.toString(num2));//[15, 43, 45, 188]
}
}
降序排序:
https://blog.csdn.net/qq_40829288/article/details/88794672