天天看点

Day05 数组

1.数组概述

  • 数组是相同类型数据的有序集合

2.数组声明创建

  1. 首先必须声明数组变量,才能使用数组
  2. 使用new操作符来创建数组
  3. 数组的元素通过索引访问,数组索引从0开始
    public class Demo01 {
    //    变量的类型 变量名 = 变量值;
    //    数组类型
        public static void main(String[] args) {
    //        1.声明数组变量
            int[] nums; //首选方法
            int nums1[]; //不是首选方法
    //        2.为数组分配空间
            nums = new int[10];
    //        3.给数组元素中赋值
            nums[0] = 1;
            nums[1] = 2;
        }
    }
               
    public class Demo02 {
        public static void main(String[] args) {
            //数组的三种初始化
    //    1.静态初始化:创建的同时给定值
            int[] a = {1,2,3};
    //    2.动态初始化:创建的时候不给定值
            int[] b = new int[10];
            b[0] = 1;
    //    3.数组的默认初始化
    //        因为数组是引用类型,它的元素相当于类的实例变量,故数组一经分配空间,
    //        每个元素也被按照实例变量同样的方式隐式初始化。即默认为0.
            
        }
    }
               
    • 数组长度是确定的,数组一旦被创建它的大小不可以改变
    • 一个数组中的元素必须是相同类型
    • 数组中的元素可以基本类型也可以是引用类型
      基本类型例如:int[] a = {1,2,3};	int[] b = new int[10]; b[0] = 1;
      引用类型例如:Man[] c = {new Man(), new Man()}; //其中Man为一个类
                 
    • 数组变量属于引用类型,数组可以看成是对象,数组中每个元素相当于该对象的成员变量
      例如使用数组时,语法为a[下标],a表示对象,[下标]表示调用数组中的成员变量
                 
    • 数组本身就是对象,Java中对象是在堆中的,因此数组无论保存原始类还是其他对象类型,数组对象本身是在堆中的。

3.数组使用

  • 普通的for循环
  • for-each循环

    例子:

    public class Demo03 {
        public static void main(String[] args) {
            int[] arrays = {1,2,3,4,5};
            int[] reverseArray = reverse(arrays);
            printArray(reverseArray);
    
        }
    
    //    反转数组
        public static int[] reverse(int[] arrays){
            int[] result = new int[arrays.length];
    
            for (int i = 0, j = arrays.length-1; i < arrays.length; i++, j--){
                result[j] = arrays[i];
            }
            return result;
        }
    //    打印数组
        public static void printArray(int[] arrays){
            for (int array : arrays) {
                System.out.print(array + " ");
            }
        }
    }
    
    //结果:5 4 3 2 1
               
  • 数组作方法入参
  • 数组作返回值

4.多维数组

java中并不常用

//创建+赋值:
int[][] array = {{1,2},{2,3},{3,4},{4,5}};
           
public class Demo04 {
    public static void main(String[] args) {
        int[][] a = {{1,2},{2,3},{3,4},{4,5}};

        for (int i = 0; i < a.length; i++) {
            for (int j = 0; j < a[i].length; j++) {
                System.out.println(a[i][j]);
            }
        }
    }
}
           

···

5.Arrays类

  • Arrays类为数组的工具类java.util.Arrays
Day05 数组
public class Demo05 {
    public static void main(String[] args) {
        int[] a = {324,467,239,75687};

        System.out.println(a); //输出 [[email protected]
        System.out.println(Arrays.toString(a)); //输出 [324, 467, 239, 75687]
        
        Arrays.sort(a);
        System.out.println(Arrays.toString(a)); //输出 [239, 324, 467, 75687]
    }
}
           

6.数组应用——冒泡排序

public class BubbleSort {
    public static void main(String[] args) {
        //    冒泡排序:从小到大排
        int[] a = {1374,438,62,3,54,9};
        int temp;

        for (int i = a.length-1; i >= 0 ; i--) {
            int flag = 0;
            for (int j = i-1; j >= 0; j--) {
                if (a[i]<a[j]){
                   temp = a[i];
                   a[i] = a[j];
                   a[j] = temp;
                   flag = 1;
                }
                else {
                    flag =0;
                }
            }
            if (flag == 0)
                continue;
        }
        System.out.println(Arrays.toString(a));
    }
}
           

7.稀疏数组

  • 一种数据结构,用于压缩数组减少内存浪费
  • 例子:
    package com.sp.array;
    
    /**
     * @Author:Sp
     * @Date:2021/1/17
     * @Description:com.sp.array
     */
    //稀疏数组例子
    public class Exercise {
        public static void main(String[] args) {
            //    创建一个二维数组
            int[][] array1 = new int[11][9];
            array1[5][8] = 1;
            array1[6][4] = 22;
    
            System.out.println("原二维数组:");
            for (int i = 0; i < array1.length; i++) {
                for (int j = 0; j < array1[i].length; j++) {
                    System.out.print(array1[i][j] + "\t");
                }
                System.out.println();
            }
    
    //        压缩
    //        1.找出有效数字、行、列为多少
            int sum = 0;
    
            for (int i = 0; i < array1.length; i++) {
                for (int j = 0; j < array1[i].length; j++) {
                    if (array1[i][j] != 0) {
                        sum++;
                    }
                }
            }
            System.out.println("有效数字个数为:" + sum);
    //        2.创建压缩数组
            int[][] array2 = new int[sum + 1][3];
            array2[0][0] = 11;
            array2[0][1] = 9;
            array2[0][2] = sum;
    
    //        3.遍历二维数组,将非0值存放在稀疏数组中
            int count = 0; //记录有效数字的个数
            for (int i = 0; i < array1.length; i++) {
                for (int j = 0; j < array1[i].length; j++) {
                    if (array1[i][j] != 0) {
                        count++;
                        array2[count][0] = i;
                        array2[count][1] = j;
                        array2[count][2] = array1[i][j];
                    }
                }
            }
    
    //            4.打印压缩数组
            System.out.println("打印压缩数组:");
            for (int i = 0; i < array2.length; i++) {
                for (int j = 0; j < array2[i].length; j++) {
                    System.out.print(array2[i][j] + "\t");
                }
                System.out.println();
            }
    
            //            解压缩
            System.out.println("解压缩:");
            int row = array2[0][0];
            int col = array2[0][1];
            System.out.println("行数为:" + row + " 列数为:" + col);
    
            int[][] array3 = new int[row][col];
    
            for (int i = 1; i < array2.length; i++) {
                array3[array2[i][0]][array2[i][1]] = array2[i][2];
            }
            //            4.打印压缩数组
            System.out.println("打印解压缩数组:");
            for (int i = 0; i < array3.length; i++) {
                for (int j = 0; j < array3[i].length; j++) {
                    System.out.print(array3[i][j] + "\t");
                }
                System.out.println();
            }
        }
    }
               
  • 原二维数组:

    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 1

    0 0 0 0 22 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 9 2

    5 8 1

    6 4 22

    解压缩:

    行数为:11 列数为:9

    打印解压缩数组:

    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 1

    0 0 0 0 22 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

    Process finished with exit code 0