Array Creation and Access · 数组的创建与访问
| English | 中文 | Pinyin · 拼音 |
|---|---|---|
| initializer list/ɪˈnɪʃəlaɪzə lɪst/ | 初始化列表 | chū shǐ huà liè biǎo |
| ArrayIndexOutOfBoundsException/əˈreɪ ˈɪndeks ˈaʊtəv baʊndz ekˈsepʃn/ | 数组下标越界异常 | shù zǔ xià biāo yuè jiè yì cháng |
Making an array
- Declare an array with a type and
[]:int[] scores; - Create it with
newand a size:int[] scores = new int[5];(five slots). - Or list the values directly:
int[] scores = {85, 90, 78};(an initializer list 初始化列表). - Every slot starts at the type's default (
0forint,nullfor objects).
造一个数组
- 用类型和
[]声明一个数组:int[] scores; - 用
new和一个大小创建它:int[] scores = new int[5];(五个槽)。 - 或直接列出值:
int[] scores = {85, 90, 78};(一个初始化列表)。 - 每个槽从类型的默认值开始(
int为0,对象为null)。
Indexing from zero
- Array elements are numbered from
0; the last index islength - 1. - Access a slot with
scores[i]:scores[0]is the first,scores[2]the third. - Assign with
scores[0] = 100;and read withint x = scores[0];. - Off-by-one on the last index is the most common array bug.
从零开始下标
- 数组元素从
0开始编号;最后一个下标是length - 1。 - 用
scores[i]访问一个槽:scores[0]是第一个,scores[2]是第三个。 - 用
scores[0] = 100;赋值,用int x = scores[0];读取。 - 在最后一个下标上差一是最常见的数组错误。
The length field
- An array's size is its
lengthfield — no parentheses:scores.length. - (Contrast
String'slength()method, which has parentheses.) lengthis fixed once the array is created — arrays don't resize.- Use
scores.lengthin loop conditions to stay in bounds.
length 字段
- 数组的大小是它的
length字段——没有括号:scores.length。 - (对比
String的length()方法,它有括号。) - 数组一旦创建,
length就固定了——数组不改变大小。 - 在循环条件里用
scores.length以保持在范围内。
Out of bounds
- A valid index is
0tolength - 1; anything else throws an ArrayIndexOutOfBoundsException 数组下标越界异常. scores[5]on a length-5 array is out of bounds (indices are0–4).- The loop condition
i < scores.lengthkeepsiin range. - Never use
<=withlength— it steps one past the end.
越界
- 有效下标是
0到length - 1;其他任何值都抛出 ArrayIndexOutOfBoundsException(数组越界异常)。 - 对一个长度为 5 的数组,
scores[5]是越界的(下标是0–4)。 - 循环条件
i < scores.length让i保持在范围内。 - 绝不要对
length用<=——它会越过末尾一步。
Array size is length (a field, NO parentheses); String length is length() (a method, WITH parentheses). Mixing them up won't compile. And valid indices are 0 to length - 1 — scores[scores.length] is one past the end and throws an ArrayIndexOutOfBoundsException. Loop with i < scores.length, never i <= scores.length.
**数组大小是 length(一个字段,没有括号);String 长度是 length()(一个方法,有括号)。**把它们弄混无法编译。而且有效下标是 0 到 length - 1——scores[scores.length] 越过末尾一步,会抛出 ArrayIndexOutOfBoundsException。用 i < scores.length 循环,绝不用 i <= scores.length。
Creating and accessing an array:
int[] a = {10, 20, 30};— length3, indices0,1,2.a[0]is10;a[2]is30;a.lengthis3.a[3]throws ArrayIndexOutOfBoundsException (no index 3).
创建并访问一个数组:
int[] a = {10, 20, 30};——长度3,下标0,1,2。a[0]是10;a[2]是30;a.length是3。a[3]抛出 ArrayIndexOutOfBoundsException(没有下标 3)。
Create an array with new type[size] or an initializer list {...}. Elements are indexed from 0 to length - 1, accessed as a[i]. The size is the length field (no parentheses). An index outside 0..length-1 throws an ArrayIndexOutOfBoundsException — loop with i < a.length.
用 new type[size] 或初始化列表 {...} 创建一个数组。元素从 0 到 length - 1 编号,用 a[i] 访问。大小是 length 字段(没有括号)。0..length-1 之外的下标会抛出 ArrayIndexOutOfBoundsException——用 i < a.length 循环。
Indexing an array from 0 · 从 0 给数组编号
a[0]=10, a[1]=20, a[2]=30; a.length is 3. · a[0]=10, a[1]=20, a[2]=30;a.length 是 3。
For int[] a = {10, 20, 30}; what is a[0]? · 对 int[] a = {10, 20, 30};,a[0] 是多少?
Arrays are indexed from 0, so a[0] is the first element, 10. · 数组从 0 编号,所以 a[0] 是第一个元素 10。
How do you get the size of an array a? · 如何获取数组 a 的大小?
Array size is the length FIELD (no parentheses). · 数组大小是 length 字段(没有括号)。
For int[] a = {10, 20, 30}; what is a.length? · 对 int[] a = {10, 20, 30};,a.length 是多少?
Three elements → length 3. · 三个元素 → 长度 3。
For a length-5 array, accessing index 5 (a[5])... · 对一个长度为 5 的数组,访问下标 5(a[5])……
Valid indices are 0..4; index 5 is out of bounds. · 有效下标是 0..4;下标 5 越界。
String uses length() (a method) while an array uses length (a field). · String 用 length()(一个方法),而数组用 length(一个字段)。
Different syntax — parentheses for String, none for arrays. · 语法不同——String 带括号,数组不带。