Java---第四章
- 一 数组
 - 基本知识
 - 数组操作
 
- 二 数组实操
 - 数组排序
 - 二分查找
 - 二维数组
 

一 数组
基本知识
概念:
 数组是编程语言中的一种常见的数据结构,能够存储一组相同类型的数据
作用:
 存储一组相同类型的数据,方便进行数理统计(求最大值,最小值,平均值以及总和),也可以进行信息的展示
定义:
public class study {
    public static void main(String[] args) {
        //1
        byte[] bytes = new byte[]{1,2,3,4,5};
        //2
        int[] numbers = {1,2,3,4,5};
    }
}
 
第一种:
- 只能在定义数组同时赋值时使用
 
第二种:
- 可以在定义数组时直接使用,也可以先定义数组,然后再赋值使用
 
数组中的默认值:
- 双精度浮点数数组中的默认值为0.0
 - 单精度浮点数数组中的默认值为0.0f
 - boolean类型数组默认元素为false
 - char类型数组中的默认元素为’\u0000’
 - 整型数组的默认值为0
 
基本要素:
 
数组操作
数组的遍历:
 将数组中的元素全部查看一遍
数组的长度----->数组.length
public class study {
    public static void main(String[] args) {
        int[] arr1 = {1,2,3,4,5};
        for(int i=0;i<arr1.length;i++){
            System.out.println(arr1[i]);
        }
    }
}
 
数组修改操作:
 案例:
 现有数列10,12,17,32,39,50,要求将该数列中所有能够被3整除的元素进行平方,然后再放回该元素所处位置
public class study {
    public static void main(String[] args) {
        int[] arr1 = {10,12,17,32,39,50};
        for(int i=0;i<arr1.length;i++){
            if(arr1[i]%3==0){
                arr1[i] *= arr1[i];
            }
        }
    }
}
 
数组添加操作
 案例:
 在某机票代售点有A,B,C,D,E,4个人正在排队购票,B的好朋友F现在也来排队购票,发现B正在排队,于是插队至B的后面,请使用数组的相关知识完成程序设计
public class study {
    public static void main(String[] args) {
        String[] arr = {"A","B","C","D","E"};
        //A B C D E
        //A B F C D E
        String[] new_arr = new String[arr.length+1];
        int co = 2;
        for(int i=0;i<co;i++){
            new_arr[i]=arr[i];
        }
        new_arr[co]="F";
        for(int j=co;j<new_arr.length;j++){
            new_arr[j+1]=arr[j];
        }
        arr = new_arr;
        for(int m=0;m<arr.length;m++){
            System.out.println(arr[m]);
        }
    }
}
 
数组删除操作
 案例1:
 在前面的案例中,购票人C因为中途有事离开,排队的人员少了一个,请使用数组的相关知识完成程序设计
public class study {
    public static void main(String[] args) {
        String[] arr = {"A","B","C","D","E"};
        //A B C D E
        //A B D E
        String[] new_arr = new String[arr.length-1];
        for(int i=0;i<2;i++){
            new_arr[i]=arr[i];
        }
        for(int j=3;j<arr.length;j++){
            new_arr[j-1]=arr[j];
        }
        arr = new_arr;
        for(int i=0;i<arr.length;i++){
            System.out.println(arr[i]);
        }
    }
}
 
数组的拷贝
 语法:
System.arraycopy(原数组,拷贝的开始位置,目标数组,存放的开始位置,拷贝的元素个数);
 
这样我们对于上面那一题里的for循环拷贝,就可以换成该语法操作
        String[] arr = {"A","B","C","D","E"};
        //A B C D E
        //A B D E
        String[] new_arr = new String[arr.length-1];
        for(int i=0;i<2;i++){
            new_arr[i]=arr[i];
        }
 
for循环换成
        System.arraycopy(arr,0,new_arr,0,2);
 
数组扩容
 语法:
数据类型[] 标识符 = Arrays.copyof(原数组,新数组的长度);
 
举例:
import java.util.Arrays;
public class study {
    public static void main(String[] args) {
        String[] arr = {"A","B","C","D","E"};
        //A B C D E
        //A B C D E F
        String[] new_arr = Arrays.copyOf(arr,arr.length+1);
    }
}
 
二 数组实操
数组排序
数组中的元素从小到大,或者从大到小的顺序依次排列。分为升序排列和降序排列
冒泡排序:
- 每一次遍历数组,都能从数组的元素中获取一个最值(最大值,最小值)
 - 在每一次遍历数组时,比较数组中相邻两个元素的大小,根据排列需求进行交换位置
 
案例:
 将数列10,70,55,80,25,60进行降序排列
public class study {
    public static void main(String[] args) {
        int[] arr = {10,70,55,80,25,60};
        for(int i=0;i<arr.length;i++){
            for(int j=0;j<arr.length-i-1;j++){
                if(arr[j]<arr[j+1]){
                    int temp = arr[j];
                    arr[j]=arr[j+1];
                    arr[j+1]=temp;
                }
            }
        }
        for(int j=0;j<arr.length;j++){
            System.out.println(arr[j]);
        }
    }
}
 
工具的排序操作:
 语法:
        Arrays.sort(数组名);//将数组中的元素进行升序排列
        Arrays.toString(数组名)//将数组中的元素组装成一个字符串
 
注:
字符能够排序,排序是按照字典的顺序进行排序(abcdefg…)
import java.util.Arrays;
public class study {
    public static void main(String[] args) {
        String[] names = {"zhangsan","zhangsi","lisi","lisan",
        "lisiabc","lisib"};
        Arrays.sort(names);
        System.out.println(Arrays.toString(names));
    }
}
 
[lisan, lisi, lisiabc, lisib, zhangsan, zhangsi]
二分查找
又称为折半查找,顾名思义,每一次都会从中间分成两个区间,利用中间元素与要查找的元素比较大小,从而确定目标元素所在的区间,依次减少范围,确定该元素
二分查找只适用于已经排好序的数组
案例:
 从数列95,93,87,86,79,72,60,53中快速找出元素60所处的位置
public class study {
    public static void main(String[] args) {
        int[] numbers = {95,93,87,86,79,72,60,53};
        int target = 60;
        int start =0;
        int end = numbers.length -1;
        while(start<end){
            int mid = (start+end)/2;
            if(numbers[mid]>target)
            {
                start = mid;
            }else if(numbers[mid]<target){
                end = mid;
            }else{
                System.out.println(mid);
                break;
            }
        }
    }
} 
 
二维数组
数组从本质上来说只有一维,二维数组是指在一维数组中再放入一个一维数组。三维数组,四维数组依次类推。

 二维数组的定义:
数据类型[][] 数组名 = new 数据类型[数组的长度][数组的长度]
 
例如:定义一个长度为5的二维数组,每一个空间中只能存放任意长度的double数组
 其中 2 也可以不填,不填就意味着可以存放任意长度的…数组
public class study {
    public static void main(String[] args) {
        double[][] as = new double[5][];
        as[0] = new double[]{12,66};
        as[1] = new double[]{17,91};
        as[2] = new double[]{15,84};
        as[3] = new double[]{14,67};
        as[4] = new double[]{19,86};
    }
}
 
案例1:
 从控制台录入5首音乐信息(包括名称,歌手,出版年月),并将其信息存储在数组中
import java.util.Scanner;
public class study {
    public static void main(String[] args) {
        String[][] music_a = new String[5][3];
        Scanner sc = new Scanner(System.in);
        for (int i=0;i<music_a.length;i++){
            System.out.println("请输入名称:");
            String name = sc.next();
            System.out.println("请输入歌手:");
            String singer = sc.next();
            System.out.println("请输入出版年月:");
            String date = sc.next();
            music_a[i] = new String[]{name,singer,date};
        }
    }
}
 
案例2:
 某学校一年级一共有3个班,第一个班10个人,第二个班8个人,第三个班7个人,现要求从控制台录入这3个班学生的成绩和年龄,并计算出每个班的平均成绩和平均年龄。
import java.util.Scanner;
public class study {
    public static void main(String[] args) {
        double[][][] stu_cal = new double[3][][];
        stu_cal[0] = new double[10][2];
        stu_cal[1] = new double[8][2];
        stu_cal[2] = new double[7][2];
        Scanner sc = new Scanner(System.in);
        //录入数据
        for(int i=0;i<stu_cal.length;i++){
            double[][] ne_cal = stu_cal[i];
            for(int j=0;j<stu_cal[i].length;j++){
                System.out.println("请输入年龄:");
                int age = sc.nextInt();
                System.out.println("请输入成绩:");
                double score = sc.nextDouble();
                ne_cal[j] = new double[]{age,score};
            }
        }
        //查看数据,并计算平均值
        for(int i=0;i<stu_cal.length;i++){
            double tolage = 0,tolscore = 0;
            double[][] ne_cal = stu_cal[i];
            for(int j=0;j<ne_cal.length;j++){
                tolage += ne_cal[j][0];
                tolscore += ne_cal[j][1];
            }
            System.out.println("第"+(i+1)+"个班的平均年龄为"+(tolage/ne_cal.length));
            System.out.println("第"+(i+1)+"个班的平均成绩为"+(tolscore/ne_cal.length));
        }
    }
}
                
















![[Day 3 of 17]Building a document scanner in OpenCV](https://img-blog.csdnimg.cn/ebf4ca234a8948de9503262c55651dae.png)

