Tạo và Truy cập Mảng
Đã giới thiệu| English | Ý nghĩa |
|---|---|
| initializer list/ɪˈnɪʃəlaɪzə lɪst/ | danh sách khởi tạo |
| ArrayIndexOutOfBoundsException/əˈreɪ ˈɪndeks ˈaʊtəv baʊndz ekˈsepʃn/ | Ngoại lệ chỉ số mảng vượt quá giới hạn |
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).
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.

A one-dimensional array (a list) with its indices and bounds
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.
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.
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.
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).
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.
indexing (truy cập chỉ số) mảng bắt đầu từ 0
a[0]=10, a[1]=20, a[2]=30; a.length là 3.
Với int[] a = {10, 20, 30}; a[0] bằng gì?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Mảng được index từ 0, nên a[0] là phần tử đầu tiên, 10.
Làm thế nào để lấy kích thước của mảng a?
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Kích thước mảng là thuộc tính length (không dùng ngoặc đơn).
Với int[] a = {10, 20, 30}; a.length bằng gì?
Độ khó 2/5 · 140 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Ba phần tử → length là 3.
Với mảng length=5, truy cập index 5 (a[5])...
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Các chỉ số hợp lệ là 0..4; index 5 nằm ngoài giới hạn.
String dùng length() (phương thức) còn mảng dùng length (thuộc tính).
Độ khó 1/5 · 100 xu + một phần thưởng nhỏ cho lần hoàn thành đầu tiên trong ngày
Cú pháp khác nhau — ngoặc đơn cho String, không có cho mảng.