数组

发布时间 2023-09-09 11:54:27作者: SimpleWord
title: 数组
index_img: img/7.svg
tags:
  - Java SE
categories:
  - Java SE
hide: false
excerpt: 数组访问、遍历、越界

概念

数组是一种容器,可以存储同种数据类型(支持隐式转换)的多个值。

定义

数据类型[] 数组名
String[] students;

初始化

一旦初始化后,数组长度就固定了,可以通过arrayName.length()获取长度。

静态初始化

明确元素。

//完整
数据类型[] 数组名 =new 数据类型[]{元素去,元素2,...}
int[] arr2 = new int[]{33, 44, 55};

//简化
数据类型[] 数组名 ={元素去,元素2,...}
String[] stduents = {"tom","cat"}

动态初始化

元素不明确(如用户输入)

数据类型[] 数组名 =new 数组类型[数组长度];
int[] arrAge = new int[50];

动态初始化数组元素的默认值:

  • 整数类型:0
  • 小数类型:0.0
  • 字符类型:‘/u0000’空格
  • boolean:false
  • 引用类型:null
public class Main {
    public static void main(String[] args) {
        String[] strings = new String[3];
        for (int i = 0; i < strings.length; i++) {
            System.out.print(strings[i] + " "); // null null null
        }
    }
}

数组的访问

数组的地址值

public class Main {
    public static void main(String[] args) {
        String[] student = {"tom", "cat", "jay"};
        System.out.println(student); //[Ljava.lang.String;@1b6d3586
    }
}

[:表示数组

L:表示数组元素是引用类型

java.lang.String:数组元素类型是 String

@:间隔符

1b6d3586:对象的哈希码的无符号十六进制。但需要注意的是,这个哈希码并不是数组在内存中的实际地址,Java 出于安全考虑,不允许直接访问对象的内存地址。

数组元素的访问

索引:0~(arrylength-1)

数组名[索引];
public class Main {
    public static void main(String[] args) {
        String[] student = {"tom", "cat", "jay"};
        System.out.println(student[0]); //tom
        String s = student[1];
        System.out.println(s); //cat
    }
}

数组元素赋值后原来的值就被替换了。

public class Main {
    public static void main(String[] args) {
        String[] student = {"tom", "cat", "jay"};
        System.out.println(student[1]); //cat
        student[1] = "liBai";
        System.out.println(student[1]); //liBai
    }
}

数组遍历

可以搭配循环

  • 打印数组元素
  • 数组计算和赋值
  • ……

IDEA中可以使用arrayName.fori快速生成结构。

public class Main {
    public static void main(String[] args) {
        String[] student = {"tom", "cat", "jay"};
        for (int i = 0; i < student.length; i++) {
            System.out.print(student[i] + " "); //tom cat jay
        }
    }
}
public class Main {
    public static void main(String[] args) {
        int[] studentAge = {12, 13, 9, 25, 24, 8};
        int var = 15;
        int sum = 0;
        for (int i = 0; i < studentAge.length; i++) {
            if (studentAge[i] < var) {
                sum++;
            }
        }
        System.out.println("年龄小于" + var + "的学生有" + sum+"个"); //年龄小于15的学生有4个

    }
}

索引越界异常

public class Main {
    public static void main(String[] args) {
        String[] student = {"tom", "cat", "jay"};
        System.out.println(student[-1]); // 索引越界
        System.out.println(student[0]);
    }
}

image-20230723143349186

二维数组

静态初始化

//完整
数据类型[][] 数组名 = new 数据类型[][]{{元素1,元素2},{元素1,元素2}...};
int[][] arr = new int[][]{{1,100},{2,98}};

//简化
数据类型[][] 数组名 = {{元素1,元素2},{元素1,元素2}...};
int[][] arr = {{1,100},{2,98}};

动态初始化

数据类型[][] 数组名 = new 数据类型[m][n];
int[][] arr = new [2][3];

获取值和遍历

public class Main {
    public static void main(String[] args) {
        int[][] arr = {{1, 100}, {2, 98}};
        System.out.println(arr[0][1]);
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[0].length; j++) {
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}

public class Main {
    public static void main(String[] args) {
        int[][] arr = new int[2][3];
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[0].length; j++) {
                arr[i][j] = 1;
                System.out.print(arr[i][j] + " ");
            }
            System.out.println();
        }
    }
}

image-20230724115810001