天天看點

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