/**
* System.arraycopy():数组复制
*
*
* src:源数组(source)
* srcPos:从源数组指定索引位置开始复制(source position)
*
* dest:目标数组(destination)
* destPos:复制的值从指定位置开始放置
*
* length:复制的元素个数
*
*
* Arrays : 数组操作的工具类
* Arrays.toString() 将数组转换为字符串
* Arrays.sort() 对数组进行升序排序
*/
//源数组
int[] src = new int[]{1,2,3,4,5};
//目标数组
int[] dest = new int[]{6,7,8,9,10,11,12,13,14,15};
System.out.println("复制前:" + Arrays.toString(src)); //[1, 2, 3, 4, 5]
System.out.println("复制前:" + Arrays.toString(dest));//[1, 2, 8, 9, 10, 11, 12, 13, 14, 15]
//复制数组System.arraycopy()
System.arraycopy(src, 0, dest, 0, src.length);
//Arrays.toString(数组):将数组转换为字符串
System.out.println(src); //[I@3654919e 保存在内存中的引用地址
System.out.println("复制后:" + Arrays.toString(src)); //[1, 2, 3, 4, 5]
System.out.println("复制后:" + Arrays.toString(dest)); //[1, 2, 8, 9, 10, 11, 12, 13, 14, 15]
/*
* 拷贝数组System.arraycopy()
*/
int[] src2 = new int[]{1,2,3,4,5}; //1.源数组
int[] dest2 = new int[src2.length]; //2.定义目标数组,长度与源数组保持一致
System.arraycopy(src2, 0, dest2, 0, src2.length);//3.将src2的值拷贝到dest2的数组中
System.out.println(Arrays.toString(dest2)); //4.输出到控制台
/*
* 数组排序:Arrays.sort() 默认升序排序
*/
int[] intArray = new int[]{12,78,32,14,3,6,90};
System.out.println("排序前:" + Arrays.toString(intArray));//[12, 78, 32, 14, 3, 6, 90]
Arrays.sort(intArray);//排序,默认升序排序
System.out.println("排序后:" + Arrays.toString(intArray));//[3, 6, 12, 14, 32, 78, 90]
//降序排序
for (int i = intArray.length-1; i >= 0; i--) { //90 78 32 14 12 6 3
int num = intArray[i];
System.out.print(num + "\t");
}
int[] array = new int[]{9,8,7,6,5,4};
/*
* 利用冒泡排序对array数组进行排序
*
* 第一轮: [8, 7, 6, 5, 4, 9]
第二轮: [7, 6, 5, 4, 8, 9]
第三轮:[6, 5, 4, 7, 8, 9]
第四轮:[5, 4, 6, 7, 8, 9]
第五轮:[4, 5, 6, 7, 8, 9]
*/
//升序
for (int i = 1; i < array.length; i++) { //比较N-1趟
//每趟两两数据进行比较
for (int j = 0; j < array.length-i; j++) {
if(array[j] > array[j+1])
{
int temp = array[j];
array[j] = array[j+1];
array[j+1] = temp;
}
}
//System.out.println(Arrays.toString(array));
}
//降序
int[] array2 = new int[]{1,2,3,4,5,6};
for (int i = 1; i < array2.length; i++) { //比较N-1趟
//每趟两两数据进行比较
for (int j = 0; j < array2.length-i; j++) {
if(array2[j] < array2[j+1])
{
int temp = array2[j];
array2[j] = array2[j+1];
array2[j+1] = temp;
}
}
System.out.println(Arrays.toString(array2));
}