11、Java数组

Java数组

数组声明创建

  1. 声明

    int[] nums;   // 声明
    
  2. 创建

    nums = new int[10];     // 创建一个数组,长度为10
    
  3. 也可以合二为一

    int[] nums = new int[10];
    

Java数组的三种初始化方式

  • 静态初始化
int[] a = {1,2,3};
Man[] mans = {new Man(1,1),new Man(2,2)};
  • 动态初始化(包含默认初始化)
int[] a = new int[2];
a[0] = 1;
a[1] = 2;
  • 默认初始化

数组元素相当于对象的成员变量,默认值跟成员变量的规则一样。数字0,布尔false,char\u0000,引用:null;

数组的四个特点

  • 数组一旦创建,大小确定,不可改变
  • 数组中元素为相同类型,不可混合
  • 数组中元素可为任意类型,包含基本类型和引用类型
  • 数组变量属于引用类型,数组本身就是对象,Java对象实在堆中,因此数组对象本身也在堆中,数组元素相当于对象的成员变量

数组边界

ArrayIndexOutOfBoundsException:数组下标越界

数组使用

  • For-Each循环

    弊端:无法获取下标

    public class Demo03 {
        public static void main(String[] args) {
            int[] arrays = {1,2,3,4,5};
            for (int array : arrays) {
                System.out.println(array);
            }
        }
    }
    
  • 数组作方法入参

    反转数组

    public class Demo03 {
        public static void main(String[] args) {
            int[] arrays = {1,2,3,4,5};
            int[] res = new int[arrays.length];
            for (int i = 0,j = res.length-1; i < arrays.length; i++,j--) {
                res[j] = arrays[i];
            }
            for (int re : res) {
                System.out.println(re);
            }
        }
    }
    
  • 数组作返回值

多维数组

public class Demo04 {
    public static void main(String[] args) {
        int[][] array = {{1,2},{2,3},{3,4,5},{6,7,8,9,10}};
        for (int i = 0; i < array.length; i++) {
            for (int j = 0; j < array[i].length; j++) {
                System.out.println(array[i][j]);
            }
        }
    }
}
int[][] a = new int[2][5]

Arrays类

数组工具类java.util.Arrays

toString & deepToString

public class Demo05 {
    public static void main(String[] args) {
        int[] a = {10,2,4,78,33,21,5,9};
        System.out.println(a);   // [I@12a3a380
        // 打印数组元素
        System.out.println(Arrays.toString(a));   // [10, 2, 4, 78, 33, 21, 5, 9]
    }
}

public class Demo02 {
    public static void main(String[] args) {
        int[][] arrays = {{1,2},{2,3},{3,4},{4,5}};
        System.out.println(Arrays.toString(arrays));
        // [[I@12a3a380, [I@29453f44, [I@5cad8086, [I@6e0be858]
        // 多维数组需要使用deepToString
        System.out.println(Arrays.deepToString(arrays));
        // [[1, 2], [2, 3], [3, 4], [4, 5]]
    }
}

sort

public class Demo05 {
    public static void main(String[] args) {
        int[] a = {10,2,4,78,33,21,5,9};
        // 数组排序
        Arrays.sort(a);
        System.out.println(Arrays.toString(a));   // [2, 4, 5, 9, 10, 21, 33, 78]
    }
}

fill

public class Demo03 {
    public static void main(String[] args) {
        int[] arrays = new int[5];
        int[] arrays1 = new int[5];
        Arrays.fill(arrays,0);
        System.out.println(Arrays.toString(arrays));
        // [0, 0, 0, 0, 0]
        Arrays.fill(arrays1,1,4,5);
        System.out.println(Arrays.toString(arrays1));
        // [0, 5, 5, 5, 0]
    }
}

equals

Arrays.equals(array, array2)
Arrays.deepEquals(deepArray1, deepArray2)  

binarySearch二分查找返回下标

https://blog.csdn.net/zhzh402/article/details/79670509

array = new int[]{0, 3, 4, 10, 20};
out.println(Arrays.binarySearch(array, 10)); //3, array必须是排序的,否则得到的是错误的结果
out.println(Arrays.binarySearch(array, 6)); //-4, 找不到的值,从-1开始,6如果存在下标是3, 所以返回-4
out.println(Arrays.binarySearch(array, 2, 5, 10)); //3, 返回的还是全局的下标值。

冒泡排序

import java.util.Arrays;

public class Demo01 {
    public static void main(String[] args) {
        int[] arrays = {23,45,6,31,74,90,5,18};
        for (int i = 0; i < arrays.length; i++) {
            for (int j = 0; j < arrays.length-1; j++) {
                int temp;
                if (arrays[j] > arrays[j+1]) {
                    temp = arrays[j];
                    arrays[j] = arrays[j+1];
                    arrays[j+1] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(arrays));
    }
}

稀疏数组(数据结构)

img

第一行,行列数+有效数字

下面行,各有效数字坐标及值

public class Demo04 {
    public static void main(String[] args) {
        //创建一个二维数组 11*11   0:没有棋子, 1:黑棋, 2:白棋
        int[][] a = new int[11][11];
        a[1][2] = 1;
        a[2][3] = 2;
        System.out.println("原数组为:");

        for (int[] x : a) {
            for (int i : x) {
                System.out.print(i + "\t");
            }
            System.out.println();
        }

        System.out.println("=============================");

        //转化为稀疏数组
        //先获取有效值的个数
        int sum = 0;
        for (int i = 0; i < 11; i++) {
            for (int j = 0; j < 11; j++) {
                if (a[i][j] != 0) {
                    sum++;
                }
            }
        }
        System.out.println("有效值的个数为:" + sum);

        //创建稀疏数组
        int[][] b = new int[sum + 1][3];
        b[0][0] = 11;
        b[0][1] = 11;
        b[0][2] = sum;

        //遍历二维数组,将非0的值存放到稀疏数组
        int count = 0;
        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                if (a[i][j] != 0) {
                    count++;
                    b[count][0] = i;
                    b[count][1] = j;
                    b[count][2] = a[i][j];
                }
            }
        }
        System.out.println("稀疏数组为:");
        for (int i = 0; i < b.length; i++) {
            System.out.println(b[i][0] + "\t" + b[i][1] + "\t" + b[i][2] + "\t");
        }

        System.out.println("=============================");
        System.out.println("还原数组:");

        //读取稀疏数组
        int[][] c = new int[b[0][0]][b[0][1]];

        //还原数组的值
        for (int i = 1; i < b.length; i++) {
            c[b[i][0]][b[i][1]] = b[i][2];
        }
        System.out.println("还原数组为:");
        for (int[] i : c) {
            for (int j : i) {
                System.out.print(j + "\t");
            }
            System.out.println();
        }

    }
}

//  原数组为:
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	1	0	0	0	0	0	0	0	0	
//  0	0	0	2	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  =============================
//  有效值的个数为:2
//  稀疏数组为:
//  11	11	2	
//  1	  2	  1	
//  2	  3	  2	
//  =============================
//  还原数组:
//  还原数组为:
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	1	0	0	0	0	0	0	0	0	
//  0	0	0	2	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
//  0	0	0	0	0	0	0	0	0	0	0	
posted @ 2021-07-23 16:05  六个柠檬  阅读(74)  评论(0)    收藏  举报