数组系列教材 (一)- Java 如何创建一个数组

更多内容,点击了解: https://how2j.cn/k/array/array-create/280.html

目录

步骤 1 : 声明数组

步骤 2 : 创建数组

步骤 3 : 访问数组

步骤 4 : 数组长度



数组是一个固定长度的,包含了相同类型数据的 容器

步骤 1 : 声明数组

int[] a; 声明了一个数组变量。
[]表示该变量是一个数组
int 表示数组里的每一个元素都是一个整数
a 是变量名
但是,仅仅是这一句声明,不会创建数组

有时候也会写成int a[]; 没有任何区别,就是你看哪种顺眼的问题

public class HelloWorld {

    public static void main(String[] args) {

        // 声明一个数组

        int[] a;

    }

}

步骤 2 : 创建数组

创建数组的时候,要指明数组的长度。 
new int[5] 
引用概念: 
如果变量代表一个数组,比如a,我们把a叫做引用 
与基本类型不同 
int c = 5; 这叫给c赋值为5 
声明一个引用 int[] a; 
a = new int[5]; 
让a这个引用,指向数组

创建数组

public class HelloWorld {

    public static void main(String[] args) {

        //声明一个引用

        int[] a;

        //创建一个长度是5的数组,并且使用引用a指向该数组

        a = new int[5];

         

        int[] b = new int[5]; //声明的同时,指向一个数组

         

    }

}

步骤 3 : 访问数组

数组下标基0
下标0,代表数组里的第一个数

访问数组

public class HelloWorld {

    public static void main(String[] args) {

        int[] a;

        a = new int[5];

         

        a[0]= 1;  //下标0,代表数组里的第一个数

        a[1]= 2;

        a[2]= 3;

        a[3]= 4;

        a[4]= 5;

    }

}

步骤 4 : 数组长度

.length属性用于访问一个数组的长度
数组访问下标范围是0到长度-1
一旦超过这个范围,就会产生数组下标越界异常

数组长度

public class HelloWorld {

    public static void main(String[] args) {

        int[] a;

        a = new int[5];

         

        System.out.println(a.length); //打印数组的长度

         

        a[4]=100//下标4,实质上是“第5个”,即最后一个

        a[5]=101//下标5,实质上是“第6个”,超出范围 ,产生数组下标越界异常

         

    }

}


更多内容,点击了解: https://how2j.cn/k/array/array-create/280.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值