การสร้างและการเข้าถึงอาร์เรย์
แนะนำใหม่
13:31
การเก็บรวบรวมข้อมูล
ถ่ายรูปหนึ่งรูปบนโทรศัพท์ของคุณ. สำหรับคอมพิวเตอร์แล้วมันไม่ใช่รูปภาพเลย — มันคือตารางของตัวเลข, ตัวเลขหนึ่งสำหรับทุกพิกเซล, ประมาณสิบสองล้านตัว. ลองทำ…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| initializer list/ɪˈnɪʃəlaɪzə lɪst/ | ลิสต์เริ่มต้นค่า |
| ArrayIndexOutOfBoundsException/əˈreɪ ˈɪndeks ˈaʊtəv baʊndz ekˈsepʃ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 初始化列表). - An array created with
newand a size starts with the type's default value in each slot (0forint,nullfor reference types); an initializer list supplies the values.
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.
การเข้าถึงองค์ประกอบของอาร์เรย์โดยเริ่มจาก 0
a[0]=10, a[1]=20, a[2]=30; a.length มีค่าเป็น 3
สำหรับ int[] a = {10, 20, 30}; a[0] คืออะไร?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
อาร์เรย์ถูกจัดอันดับตัวชี้ด้วย 0 ดังนั้น a[0] จึงเป็นองค์ประกอบแรก, 10
คุณจะได้ขนาดของอาร์เรย์ a อย่างไร?
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ขนาดอาร์เรย์คือฟิลด์ length (ไม่มีวงเล็บ)
สำหรับ int[] a = {10, 20, 30}; a.length คืออะไร?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
สามองค์ประกอบ → length เป็น 3
สำหรับอาร์เรย์ขนาด 5 การเข้าถึงดัชนี 5 (a[5])...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ตัวชี้ที่ถูกต้องคือ 0..4; ตัวชี้ 5 อยู่นอกขอบเขต
String ใช้ length() (เมทოდ) ในขณะที่อาร์เรย์ใช้ length (ฟิลด์)
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ไวยากรณ์ต่างกัน — มีวงเล็บสำหรับ String ไม่มีสำหรับอาร์เรย์
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้