JavaSE - 数组

1. 数组的基本概念

1.1 什么是数组

数组是一组相同类型数据的集合

1.2 数组的定义与初始化

public class TestDemo {

    public static void main(String[] args) {
        // 1.
        int[] arr2 = new int[]{1,2,3};
        // 2.
        int[] arr1 = {1,2,3};
        // 3.
        int[] arr3 = new int[3];
    }
    
}

在java中, 有3种定义数组的方式,如上代码

首先看1,int[] arr2 = new int[]{1,2,3};

int[] 表示数组中存储的数据类型是int整形,arr2为数组名,new int[]{1,2,3}表示创建一个数组对象,这个数组存储3个整形数据分别是1,2,3

2实际上与1是一样的,只是2在语法上简化了,为了方便使用

3和1类似,new int[3] 只是定义了数组长度,但是并没有对数组中的数据进行初始化

如果没有对数组进行初始化,数组中元素有其默认值,如下图

下面这张表是,如果数组中存储元素类型为基本数据类型,每个数组元素的默认值

1.3 数组的使用

访问数组

与C语言相同,在java中可以使用[],对数组进行访问

public class TestDemo {
    public static void main(String[] args) {
        int[] arr2 = new int[]{1,2,3};
        System.out.println(arr2[0]);
        System.out.println(arr2[1]);
        System.out.println(arr2[2]);
    }
}

数组的遍历

下面介绍3种在java中遍历数组的方法

1. for循环遍历

public class TestDemo {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
    }
}

这里需要注意的是,数组名.length就可以得到一个数组中元素的个数

2. for-each 遍历

public class TestDemo {
    public static void main1(String[] args) {
        int[] arr = {1,2,3,4,5};
        /*for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }*/
        for (int x : arr) {
            System.out.print(x + " ");
        }
    }
}

注意:for-each只适用于打印数组

3. 数组转字符串打印

import java.util.Arrays;
public class TestDemo {
    public static void main(String[] args) {
        int[] arr = {1,2,3,4,5};
        /*for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }*/
        /*for (int x : arr) {
            System.out.print(x + " ");
        }*/

        String str = Arrays.toString(arr);
        System.out.println(str);

    }
}

使用Arrays这个类中的toSring方法对数组arr转换为字符串进行打印

2. 引用

2.1 什么是引用

基本类型变量与引用类型变量的区别

2.2 例子

public class TestDemo {
    public static void func() {
        int[] array1 = new int[3];
        array1[0] = 10;
        array1[1] = 20;
        array1[2] = 30;

        int[] array2 = new int[]{1,2,3,4,5};
        array2[0] = 100;
        array2[1] = 200;

        array1 = array2;
        array1[2] = 300;
        array1[3] = 400;
        array2[4] = 500;
        for (int i = 0; i < array2.length; i++) {
            System.out.println(array2[i]);
        }
    }

    public static void main(String[] args) {
        func();
    }
}

3. 数组传参

数组作为实参时,向方法内部传递的是引用变量存储的对象地址,如下图

4. 二维数组

4.1 二维数组的定义

数据类型[][] 数组名称 = new 数据类型 [行数][列数] { 初始化数据 };

public class TestDemo {
    public static void main(String[] args) {
        int[][] arr1 = {{1,2,3},{4,5,6}};
        int[][] arr2 = new int[][]{{1,2,3},{4,5,6}};
        int[][] arr3 = new int[2][3];
    }
}

4.2 深入理解二维数组

二维数组实际上是一个特殊的一维数组

下面证明

public class TestDemo {
    public static void main(String[] args) {
        int[][] arr1 = {{1,2,3},{4,5,6}};
        System.out.println(arr1[0]);
        System.out.println(arr1[1]);
    }
}

由于,二维数组存储的是数组对象的地址,所以二维数组在定义的时候,可以不指定列,但是一定得指定行,因为行指定二维数组存储多少个数组对象地址,如下所示

public class TestDemo {
    public static void main(String[] args) {
       int[][] arr = new int[2][];
       arr[0] = new int[]{1,2,3};
       arr[1] = new int[]{1,2,3,4,5};
    }
}

4.3 遍历二维数组

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值