内存空间

        栈内存的特点:存储的都是局部变量(函数内声明的变量),变量出了作用域后自行释放内存空间。

        堆内存的特点:存储的都是对象数据,使用完成后不会马上消失,需要垃圾回收器回收释放内存空间。

        :凡是new关键字创建的对象,都在堆空间里。

常见问题

        NullPointerException:空指针错误,引用类型变量没有指向任何内存地址,却调用了对象的属性或方法

        ArrayIndexOutOfBoundsException:索引值越界,访问了不存在的索引值

找最大值

public static int GetMax(int[] arr) {
	int max = arr[0]; 
	for(int i = 1 ; i < arr.length ; i++) {
		if(arr[i]>max) {
			max = arr[i];
		}
	}
	return max;
}

排序算法

           选择排序(直接排序):使用一个元素与其他元素挨个比较,符合条件交换位置

public static void SelectSort(int[] arr)
{
	for(int j = 0; j<arr.length-1; j++){  
		for(int i = j+1 ; i<arr.length ; i++){ 
			if(arr[i]>arr[j]){
				int temp = arr[i];
				arr[i] = arr[j];
				arr[j] = temp;
			}
		}
	}
}

           冒泡排序:使用相邻的两个元素挨个比较一次,符合条件交换位置

public static void BubbleSort(int[] arr) {
    for(int j = 0 ; j<arr.length-1 ; j++) { 
	    for(int i = 0 ; i<arr.length-1-j  ; i++) {
            if(arr[i]>arr[i+1]) {
				int temp  = arr[i];
				arr[i] = arr[i+1];
				arr[i+1] = temp;
			}
		}
	}
}

数组的特点

         1. 数组只能存储同一种数据类型;
         2. 数组给存储到数组中的元素分配一个索引值,索引值从0开始,最大的索引值是length-1;
         3. 数组一旦初始化,长度固定;
         4. 数组中的元素与元素之间的内存地址是连续的。

收藏 打印