笔记
数组:存放一组数据,数组长度是定长的。
语法:数据类型[]数组名称 = new 数据类型[num];
Num:表示数组的长度,是一个数值
变化:数据类型数组名称[] = new 数据类型[num];
数据类型[]数组名称 = new 数据类型[]{数值1,数值2,....};
数据类型[]数组名称 = {数值1,数值2,....};
栈内存:存放的是函数(方法)中产生的基本数据类型的变量和对象、数组的引用。
数组通过下标(脚标)进行访问。
Arr4
1 | 2 | 3 |
0 | 1 | 2 |
数组的长度:arr.length
下标的最大值:arr.length-1
堆内存:存放对象和数组的实例
回收机制:一旦超出作用范围,就会自动释放,销毁回收内存空间,栈内存是由虚拟机自动释放空间
堆内存:GC进行定时扫描内存空间,如果数据没有被引用,则释放
遍历数组;通过循环
一维数组
二维数组:数据类型[][] 数组名称 = new 数据类型[n][m];
数据类型 数组名称[][] = new 数据类型[n][m];
n:表示有n个一维数组
M表示每个一维数组的长度为m 。。。Arr1=new int[4][3]
1 00 | 2 01 | 3 02 |
1 10 | 2 11 | 3 12 |
1 | 2 | 3 |
1 | 2 31 | 3 32 |
数组中的最大值和最小值
冒泡算法 升序
比较几次 | ||||||
3 | 4 | 5 | 1 | 2 | ||
3 | 4 | 1 | 2 | 5 | 4 | |
3 | 1 | 2 | 4 | 5 | 3 | |
1 | 2 | 3 | 4 | 5 | 2 |
3 | 4 | 1 | 6 | 2 | 5 | |
3 | 1 | 4 | 2 | 5 | 6 | 5 |
1 | 3 | 2 | 4 | 5 | 4 | |
1 | 2 | 3 | 4 | 5 | 3 | |
1 | 2 | 3 | 2 | |||
1 | 2 | 1 |
外循环循环5次,内部循环依次循环(5,4,3,2,1)次
二分法:折半查找
先条件:升序、降序
1-100 50-100 1-50
import java.util.Arrays;
public class Day03_01{
public static void main(String[] args){
//八种基本数据类型的默认值,控制台输出
//存放的是默认值
int[] arr1 = new int[5];//建议使用
int arr2[] = new int[5];
int[] arr3 = new int[]{1,2,3};
int[] arr4 = {1,2,3};
arr4[0] = 10;
//System.out.println(arr[4]);
/*有问题的写法
int[] arr5 ;
arr5={1,2,3};
*/
/*int a = 10;
int b = 10;
if(int a>1){
int a;
}*/
/*for(int a = 0; a<5;a++){
System.out.println(a);
}*/
/*int a = 10;
System.out.println("for之外:"+a);*/
//null:空地址
/*int[] arr6 = null;
System.out.println(arr6[0]);*/
int[] arr7 = {1,9,2,3,4,5,6,8};
/*for(int i = 0; i < arr7.length ; i++){
System.out.println(arr7[i]);
}*/
Arrays.sort(arr7);//为什么啥也不输出?
//System.out.println(Arrays.toString(arr7));
}
}
import java.util.Arrays;
public class Day03_02{
public static void main(String[] args){
//四种定义二维数组的形式
int[][] arr1 = new int[2][3];//建议使用
int arr2[][] = new int[2][3];
int[][] arr3 = new int[][]{{1,2},{3,4},{5,6,7}};//可以定义不规则的二维数组
int[][] arr4 = {{1,2},{3,4},{5,6}};
int[] arr6[] = {{1,2},{3,4},{5,6}};
//System.out.println(arr4[0][0]);
//二维数组怎么遍历
/*for(int i = 0 ;i< arr3.length; i++){
for(int j = 0; j< arr3[i].length; j++){
System.out.println(arr4[i][j]);
}
}*///Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 2 at Day03_02.main(Day03_02.java:18)
/*for(int i = 0 ;i< arr4.length; i++){
System.out.println(Arrays.toString(arr4[i]));
}*/
int a,b;
int[] x ={1} ,y[]={{1,2},{3,4}};
System.out.println(x+".........."+y);
for(int i = 0 ;i< y.length; i++){
System.out.println(Arrays.toString(y[i]));
}
}
}
import java.util.Arrays;
public class Day03_03{
public static void main(String[] args){
int[] arr1 = {1,2,3,4,5,6};
//通过值的比较进行最值寻找
/*int max = 0;
for(int i = 0; i< arr1.length; i++){
//arr1[i];
if(max < arr1[i]){
max = arr1[i];
}
}
System.out.println(max);*/
int index = 0;//下标
for(int i = 0; i< arr1.length; i++){
if(arr1[index] < arr1[i]){
index = i;
}
}
System.out.println(arr1[index]);
}
}
import java.util.Arrays;
public class Day03_04{
public static void main(String[] args){
//冒泡算法
int[] arr1 = {3,4,1,6,2,5};
for(int i = 0; i< arr1.length-1; i++){
System.out.println("i:"+i);
for(int j = 0;j< arr1.length-1-i;j++){
if(arr1[j] > arr1[j+1]){
int t = arr1[j];
arr1[j] = arr1[j+1];
arr1[j+1] = t;
}
}
System.out.println(arr1[j]);//不对
}
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
}
}
import java.util.Arrays;
public class Day03_05{
public static void main(String[] args){
//折半查找
int[] arr1 = {3,4,1,6,2,5};
Arrays.sort(arr1);//升序
int mid = arr1.length/2;
int min = 0 ;
int max = arr1.length-1;
int a = 5;
while(arr1[mid] != a){//不知道循环次数,选择while循环
if(arr1[mid] > a){
max = mid-1;
}
if(arr1[mid] < a){
min = mid+1;
}
mid = (min+max)/2;
}
//int b = Arrays.binarySearch(arr1,5);
//System.out.println("b:"+b);
if(mid == -1){
System.out.println("不存在");
}else{
System.out.println("存在,在第"+mid+"位");
}
System.out.println();
}
}
import java.util.Arrays;
public class A01{
public static void main(String[] args){
/*int[] arr1 = {1,2,3};
arr1[0] = 5;
//System.out.println(arr1[0]);
int[] arr2 = null;
//System.out.println(arr2[0]);
short[] arr3 = new short[5];
System.out.println("short");
for(int i = 0 ; i < arr3.length ; i++){
System.out.println(arr3[0]);
//System.out.println(Arrays.toString(arr3));
}*/
/*int[][] arr1 = new int[2][3];
int arr2[][] = new int[2][3];
int [][] arr3 = new int[][] {{1,2},{3,4},{5,6,7}};
int[][] arr4 = {{1,2},{3,4},{5,6,7}};
int[] arr6[] = {{1,2},{3,4},{5,6,7}};*/
//System.out.println(arr4[0][0]);
/*for(int i = 0;i<arr4.length;i++)
for(int j = 0;j< arr4[i].length;j++){
System.out.println(arr4[i][j]);
}
for(int i = 0;i<arr4.length;i++)
System.out.println(Arrays.toString(arr4[i]));*/
/*int[] arr1 = {1,2,3,4,5,6,7,8};
int max = 1;
for(int i = 0;i< arr1.length; i++){
if(max < arr1[i]){
max =arr1[i];
}
}
System.out.println(max);*/
int[] arr1 = {5,2,6,2,7,1,3,8};
/*for(int i = 0; i<=arr1.length-1;i++){
System.out.println("i:"+i);
for(int j = 0;j< arr1.length-1-i;j++){
if(arr1[j] > arr1[j+1]){
int t = arr1[j];
arr1[j] = arr1[j+1];
arr1[j+1] = t;
}
}
}*/
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
}
}