目录
1.8 System.arraycopy()数组复制(比循环复制快)
prev>>>JAVA语法基础1(入门手册)
1.数组
1.1 定义
1.2 动态初始化
1.3 数组元素访问
1.4 内存分配
栈内存 用完即释放 定义方法的变量
堆内存 用完在垃圾回收器空闲回收 而非用完即回收(隐患)
重点(垃圾回收)
package com.basic_01;
public class c1 {
public static void main(String[] args) {
int[] arr= new int[3];
int[] arr2= new int[4];
System.out.println(arr);//[I@723279cf 为arr的地址位置
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println(arr2);
System.out.println(arr2[0]);
System.out.println(arr2[1]);
System.out.println(arr2[2]);
System.out.println(arr2[2]);
arr[0] = 1;
arr[2] = 9;
arr2[0] = -1;
arr2[2] = 2;
arr2[3] = 2;
System.out.println(arr);
System.out.println(arr[0]);
System.out.println(arr[1]);
System.out.println(arr[2]);
System.out.println(arr2);
System.out.println(arr2[0]);
System.out.println(arr2[1]);
System.out.println(arr2[2]);
System.out.println(arr2[2]);
}
}
1.5静态初始化
1.6小问题
1.7数组常见操作
遍历
最值
ds基础 冒泡排序 (较为复杂 后面会详细讲解)
package com.basic_01;
public class c1 {
public static void main(String[] args) {
//n^2 sort 冒泡
int[] arr = {1,4,2,3,9,3};
int[] sorted = new int[arr.length];
//数组复制
for(int ls=0;ls<arr.length;ls++){
sorted[ls] = arr[ls];
}
for(int j=0;j<sorted.length-1;j++){
for(int i =j;i<sorted.length-1;i++){
if(sorted[i]>sorted[i+1]){
int temp = sorted[i];
sorted[i]=sorted[i+1];
sorted[i+1]=temp;
}
}
}
for(int out=0;out <sorted.length;out++){
System.out.println(sorted[out]);
}
}
}
1.8 System.arraycopy()数组复制(比循环复制快)
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
代码解释:
Object src : 原数组
int srcPos : 从元数据的起始位置开始
Object dest : 目标数组
int destPos : 目标数组的开始起始位置
int length : 要copy的数组的长度
package com.basic_01;
public class c1 {
public static void main(String[] args) {
//拼接 systemarraycopy
int[] arry1 = {1,2,3};
int[] arry2 = new int [3];
System.arraycopy(arry1,0,arry2,0,3);
for(int i = 0;i<arry2.length;i++){
System.out.println(arry2[i]);
}
}