ArrayList: a list that grows · ArrayList: ลิสต์ที่ขยายขนาดได้
A list that can grow
- An array has a fixed size. Once you make it, the length cannot change.
- An
ArrayListis a list that can grow and shrink while the program runs. - You must import it at the top of the file:
import java.util.ArrayList;.
รายการที่สามารถขยายขนาดได้
- Array มี ขนาดคงที่ เมื่อสร้างแล้ว ความยาวไม่สามารถเปลี่ยนแปลงได้
ArrayListคือรายการที่สามารถ ขยาย และ หด ขนาดได้ในขณะโปรแกรมทำงาน- คุณต้อง import ไว้ที่ด้านบนของไฟล์:
import java.util.ArrayList;
Making an ArrayList
- Write the type of the items inside
<...>. This is the AP CSA way. ArrayList<String>holds text.ArrayList<Integer>holds whole numbers.- A new list starts empty, with size
0.
การสร้าง ArrayList
- เขียนชนิดของสิ่งของภายใน
<...>นี่คือวิธีมาตรฐานของ AP CSA ArrayList<String>เก็บข้อความArrayList<Integer>เก็บจำนวนเต็ม- รายการใหม่จะเริ่ม ว่างเปล่า โดยมีขนาด
0
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> names = new ArrayList<String>();
System.out.println(names.size()); // 0
names.add("Ann");
names.add("Bo");
System.out.println(names.size()); // 2
System.out.println(names); // [Ann, Bo]
}
}
The five methods you need
list.size()— how many items are in the list.list.add(x)— putxat the end of the list.list.get(i)— the item at positioni(the first position is 0).list.set(i, x)— replace the item at positioniwithx.list.remove(i)— take out the item at positioni; later items shift left.
5 methods ที่คุณจำเป็นต้องรู้
list.size()— จำนวนองค์ประกอบในรายการlist.add(x)— ใส่xเข้าไปที่ ท้าย ของรายการlist.get(i)— องค์ประกอบที่ตำแหน่งi(ตำแหน่งแรกคือ 0)list.set(i, x)— เปลี่ยนองค์ประกอบที่ตำแหน่งiด้วยxlist.remove(i)— ลบองค์ประกอบที่ตำแหน่งi; องค์ประกอบถัดไปจะเลื่อนไปทางซ้าย
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<String> fruit = new ArrayList<String>();
fruit.add("apple");
fruit.add("pear");
fruit.add("plum");
System.out.println(fruit.get(0)); // apple
fruit.set(1, "grape"); // pear -> grape
fruit.remove(2); // take out "plum"
System.out.println(fruit); // [apple, grape]
}
}
Numbers need Integer
- An
ArrayListcan only hold objects, not the simple typeint. - So we write
ArrayList<Integer>.Integeris the object form ofint. - Java changes between
intandIntegerfor you. This is called autoboxing. - You can
add(7)anint, andget(0)gives back a value you can use asint.
ตัวเลขต้องใช้ Integer
- An
ArrayListสามารถเก็บได้เฉพาะ objects เท่านั้น ไม่สามารถเก็บ primitive type อย่างintได้โดยตรง - ดังนั้นเราจึงเขียน
ArrayList<Integer>.Integerคือรูปแบบ object ของint - Java จะแปลงระหว่าง
intและIntegerให้โดยอัตโนมัติ สิ่งนี้เรียกว่า autoboxing - คุณสามารถ
add(7)一个int, และget(0)จะคืนค่ากลับมาที่คุณสามารถใช้แทนintได้
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(10); // int 10 is boxed into Integer
nums.add(20);
int first = nums.get(0); // unboxed back to int
System.out.println(first + nums.get(1)); // 30
}
}
Two ways to traverse
- An index for-loop uses positions
0tosize() - 1. Use it when you need the index. - An enhanced for-loop (
for (int x : list)) reads each item in turn. Use it when you only need the values. - Both visit every item once, in order.
การ traverse แบบสองวิธี
- index for-loop ใช้ตำแหน่ง
0ถึงsize() - 1ใช้งานเมื่อคุณต้องการ index - enhanced for-loop (
for (int x : list)) อ่านแต่ละองค์ประกอบตามลำดับ ใช้งานเมื่อคุณต้องการเพียงค่าเท่านั้น - ทั้งสองวิธีจะเยี่ยมชมทุกองค์ประกอบหนึ่งครั้งตามลำดับ
import java.util.ArrayList;
public class Main {
public static void main(String[] args) {
ArrayList<Integer> nums = new ArrayList<Integer>();
nums.add(3);
nums.add(5);
nums.add(8);
// index for-loop
for (int i = 0; i < nums.size(); i++) {
System.out.println("at " + i + ": " + nums.get(i));
}
// enhanced for-loop
int total = 0;
for (int x : nums) {
total = total + x;
}
System.out.println("total " + total); // total 16
}
}
Common mistakes
ArrayListuses.add,.get(i),.size()— not[].- Store objects: use
ArrayList<Integer>, notArrayList<int>.
ข้อผิดพลาดที่พบบ่อย
ArrayListใช้.add,.get(i),.size()— ไม่ใช่[]- เก็บ objects: ใช้
ArrayList<Integer>, ไม่ใช่ArrayList<int>
Now you try
- Each task pre-fills the class skeleton — write your code inside main, or complete the method shown.
- Press Run to compile and run, then Check answer.
- Your code compiles and runs on the server, so even the first run is fast.
ลองดูเลย
- Each task เติม skeleton class ไว้ล่วงหน้า — เขียนโค้ดของคุณ ภายใน main, หรือเติมเต็ม method ที่แสดง
- กด Run เพื่อ compile และ run, แล้วกด Check answer
- โค้ดของคุณ compile และ run บน server, ดังนั้นแม้การ run ครั้งแรกก็เร็ว
An ArrayList grows · ArrayList ขยายขนาดได้
Unlike an array, an ArrayList changes size as you add/remove. · ต่างจาก array, ArrayList เปลี่ยนขนาด เมื่อคุณเพิ่ม/ลบข้อมูล.
Complete sumList(ArrayList<Integer> a) so it returns the total of all numbers in the list. An empty list totals 0. Traverse with a loop. · เติม sumList(ArrayList<Integer> a) ให้สมบูรณ์เพื่อให้กลับคืน ผลรวม ของตัวเลขทั้งหมดใน list. Listว่างมีผลรวม 0.遍历 ด้วย loop.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete countUp(int n) so it makes a new ArrayList<Integer>, adds the numbers 1, 2, ..., n in order, and returns it. If n is 0, return an empty list. · เติม countUp(int n) ให้สมบูรณ์เพื่อสร้าง ArrayList<Integer> ใหม่, เพิ่มตัวเลข 1, 2, ..., n按照顺序, และกลับคืนมัน. หาก n เป็น 0, กลับคืน list ว่าง.
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
Complete doubleAt(ArrayList<Integer> a, int i). Read the value at index i with get, then use set to store two times that value back at index i. Return nothing (void). · เติม doubleAt(ArrayList<Integer> a, int i) ให้สมบูรณ์. อ่านค่าที่ index i ด้วย get, จากนั้นใช้ set เพื่อเก็บ สองเท่า ของค่านั้นกลับไปที่ index i. กลับคืนอะไรเลย (void).
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่