1-D arrays: store, index, and traverse · 1-D arrays: การเก็บข้อมูล, ดึงใช้ด้วย index, และการ遍历 (traverse)
Many values in one box
- An array holds many values of the same type in order.
- Until now one variable held one value. An array holds a whole list.
- For example, the test scores of a class can live in one
intarray.
ค่าหลายค่าในกล่องเดียว
- Array เก็บหลายค่าของ ประเภทเดียวกัน按照顺序
- จนกระทั่งตอนนี้ ตัวแปรหนึ่งเก็บค่าหนึ่ง Array เก็บรายการทั้งหมด
- ตัวอย่างเช่น คะแนนสอบของชั้นเรียนสามารถอยู่ใน array
intตัวเดียว
Make an array with values
- If you know the values, list them in curly braces
{ }. - The type is the element type plus
[], likeint[]. - Java counts how many values you gave and makes the array that size.
สร้าง array พร้อมค่า
- หากคุณทราบค่าแล้ว ให้ระบุ它们在花括号
{ } - ประเภทคือประเภทองค์ประกอบบวก
[]เช่นint[] - Java นับจำนวนค่าที่คุณให้ไว้และสร้าง array ตามขนาดนั้น
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
System.out.println(scores[0]); // 90
System.out.println(scores[2]); // 70
}
}
Index positions
- Each value has an index (its position). Indexes start at 0.
- So the first value is
scores[0], the second isscores[1], and so on. - You can also change a value:
scores[1] = 85;.
ตำแหน่ง Index
- ค่าแต่ละค่ามี index (ตำแหน่ง) Indexes เริ่มที่ 0
- ดังนั้นค่าแรกคือ
scores[0], ค่าที่สองคือscores[1], และ seterusnya - คุณยังสามารถ เปลี่ยน ค่าได้:
scores[1] = 85;
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
scores[1] = 85; // change the middle value
System.out.println(scores[1]); // 85
}
}
Make an empty array
- Use
new int[n]to make an array ofnvalues. - Every
intslot starts at 0 automatically. - Then you fill the slots one by one.
สร้าง array ว่าง
- ใช้
new int[n]เพื่อสร้าง array ของnค่า - ทุกช่อง
intเริ่มที่ 0 อัตโนมัติ - จากนั้นคุณเติมช่องทีละช่อง
public class Main {
public static void main(String[] args) {
int[] nums = new int[3]; // {0, 0, 0}
nums[0] = 5;
nums[1] = 6;
System.out.println(nums[0] + " " + nums[1] + " " + nums[2]); // 5 6 0
}
}
.length
array.lengthtells you how many values the array has.- Note: no parentheses — it is
scores.length, notscores.length(). - The last valid index is always
length - 1.
.length
array.lengthบอกว่าคุณมีค่า多少个ใน array- หมายเหตุ: ไม่มีวงเล็บ — มันเป็น
scores.lengthไม่ใช่scores.length() - Index ที่ถูกต้องสุดท้ายคือ
length - 1เสมอ
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
System.out.println(scores.length); // 3
System.out.println(scores[scores.length - 1]); // 70 (last value)
}
}
Traverse with a for loop
- To visit every value, loop the index from
0up tolength - 1. - Use
i < array.lengthas the stop test. - Below we add up every value.
Traverse ด้วยลูป for
- เพื่อ访问 every value, ลูป index จาก
0ไปจนถึงlength - 1 - ใช้
i < array.lengthเป็นการทดสอบหยุด - ด้านล่างนี้ เราผลรวม every value
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
int total = 0;
for (int i = 0; i < scores.length; i++) {
total = total + scores[i];
}
System.out.println(total); // 240
}
}
Enhanced for loop
- The enhanced for (or "for-each") visits each value directly.
- Read it as: "for each
int vinscores". - Use it when you only need the values, not the index.
Enhanced for loop
- The enhanced for (หรือ "for-each")访问每个值reading directly.
- อ่าน它以: "สำหรับแต่ละ
int vในscores" - ใช้เมื่อคุณต้องการเพียง ค่า, ไม่ต้องการ index
public class Main {
public static void main(String[] args) {
int[] scores = {90, 80, 70};
int total = 0;
for (int v : scores) {
total = total + v;
}
System.out.println(total); // 240
}
}
Common mistakes
a.lengthhas no(); valid indexes run0toa.length - 1.- Going out of range throws ArrayIndexOutOfBoundsException.
ข้อผิดพลาดที่พบบ่อย
a.lengthไม่มี(); valid indexes RUNNING0ถึงa.length - 1- การเข้า out of range会引起ArrayIndexOutOfBoundsException
Now you try
- Some tasks print a result; others complete a method the Harness calls.
- Remember: indexes start at 0, and the last index is
length - 1. - Press Run to compile, then Check answer.
ลองดูเลย
- บาง任务print一个结果; others完成一个方法the Harness调用
- จำไว้ว่า: indexes เริ่มที่ 0, และ index สุดท้ายคือ
length - 1 - กด Run เพื่อคอมไพล์ แล้วกด Check answer
A 1-D array, indexed · 1-D array ที่มี index
An array stores values in order; reach one with a[i]. · Array เก็บค่าเรียงลำดับ; เข้าถึงแต่ละตัวด้วย a[i].
The array nums is already filled. Use a loop to add up all its values and print the total on one line. For {4, 8, 15, 16, 23, 42} the output is 108. · array nums ถูกเติมค่าแล้ว ใช้ loop เพื่อบวกค่าทั้งหมดเข้าด้วยกันและพิมพ์ผลลัพธ์รวมบนบรรทัดเดียว. สำหรับ {4, 8, 15, 16, 23, 42} ผลลัพธ์คือ 108.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete sum(int[] a) so it returns the sum of all values in the array. An empty array should return 0. The checker calls it with several arrays. · เติม sum(int[] a) ให้สมบูรณ์เพื่อให้กลับคืนผลรวมของค่าทั้งหมดใน array. Arrayว่างควรกลับคืน 0. ตัวตรวจสอบเรียกใช้กับ array หลายชุด.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete doubleAll(int[] a). It does not return anything. Instead it changes the array in place so every value becomes twice as big. {1, 2, 3} becomes {2, 4, 6}. · เติม doubleAll(int[] a) ให้สมบูรณ์. มันไม่ return อะไรเลย แต่จะแก้ไข array โดยตรงทำให้ทุกค่ามีขนาดเป็นสองเท่า. {1, 2, 3} จะกลายเป็น {2, 4, 6}.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่