赞
踩
每一个存储到数组的元素,都会自动的拥有一个编号,从0开始。 这个自动编号称为数组索引(index),可以通过数组的索引访问到数组中的元素
数组名[索引];
示例代码
- package com.itheima.array;
-
- public class Demo3ArrayIndex {
- /*
- 数组动态初始化:
- 初始化的时候, 手动指定数组长度, 系统会为数组容器分配初始值.
- 数组的元素访问格式:
- 数组名[索引]
- 索引: 数组中数据的编号方式, 编号从0开始
- 作用: 访问数组容器中的空间位置
- 注意:
- 数组在创建完毕后, 即使没有赋值, 也可以取出, 但取出的元素都是默认初始化值.
- */
- public static void main(String[] args) {
- int[] arr = new int[3]; // 0 1 2
- System.out.println(arr); // 数组的内存地址 [I@10f87f48
-
- // 数组名[索引] 访问数组容器中的空间位置
- System.out.println(arr[0]); // 0 系统自动分配的默认初始化值
- System.out.println(arr[1]);
- System.out.println(arr[2]);
-
- System.out.println("--------------");
-
- // 数组名[索引]
- arr[0] = 11;
- arr[1] = 22;
- arr[2]
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。