การ Implement Algorithms ของ ArrayList
แนะนำใหม่
13:31
การเก็บรวบรวมข้อมูล
ถ่ายรูปหนึ่งรูปบนโทรศัพท์ของคุณ. สำหรับคอมพิวเตอร์แล้วมันไม่ใช่รูปภาพเลย — มันคือตารางของตัวเลข, ตัวเลขหนึ่งสำหรับทุกพิกเซล, ประมาณสิบสองล้านตัว. ลองทำ…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| shift/ʃɪft/ | การเลื่อน |
| iteration/ˌɪtəˈreɪʃn/ | Repeating a sequence of instructions or calculations. |
| delete/dɪˈliːt/ | ลบ |
| mutable objects | |
| insert/ˈɪnsɜːt/ | แทรก |
| shifted left | |
| shifts left | |
| shifts left |
การ์ดนี้เป็นการ์ดใหม่ จึงแสดงเป็นภาษาอังกฤษจนกว่าคำแปลจะพร้อม
Adjacent removals reveal a skipped element
- Starting with
[0, 0, 5], remove index 0 and then increment i. The second zero has shifted 左移 into index 0, so that forward loop skips it and leaves[0, 5]. - For the forward while-loop taught here, stay at the same index after removal and advance only when keeping an element. It then removes both zeros and finishes with
[5].
![Adjacent removals reveal the skipped-element bug: scanning [0, 0, 5] forwards, removing index 0 and then incrementing skips the second zero, which moved to index 0; keep the same index after a removal and increment only when keeping an item, giving [5]](/learn-media/ap_computer_science_a/assets/0410-skip-bug.png?v=1794225445)
รักษาดัชนีไปข้างหน้าปัจจุบันหลังจากลบ
- หลังจาก
remove(i)องค์ประกอบทั้งหมดถัดไปจะเลื่อนไปทางซ้ายหนึ่งตำแหน่ง หากองค์ประกอบใดตั้งอยู่ที่ i ปัจจุบัน ยังไม่ได้ถูกตรวจสอบ ให้ตรวจสอบอีกครั้งในรอบถัดไป - ลูปด้านล่างรับลิสต์ของ Integer ที่ไม่เป็น null และไม่มีความยาวเป็นศูนย์ แล้วลบเลข 0 ทั้งหมดออกในตำแหน่งเดิม โดยใช้ขนาดปัจจุบันในแต่ละรอบและไม่ได้เรียกใช้ get กับลิสต์ที่เป็นว่าง
import java.util.ArrayList;
public class RemoveZeros {
public static void removeZeros(ArrayList<Integer> list) {
int i = 0;
while (i < list.size()) {
if (list.get(i) == 0) {
list.remove(i);
} else {
i++;
}
}
}
}
อธิบายว่าทำไมลูปนี้จึงหยุดทำงาน
- ในแต่ละรอบจะทำการลบองค์ประกอบหนึ่งออกและลดขนาดลง หรือเก็บไว้แล้วเพิ่มค่า i ดังนั้น
size() - iจำนวนตำแหน่งที่ยังต้องตรวจสอบ จะลดลงหนึ่งทุกครั้งที่วนลูป - เริ่มจาก
[0, 0, 5]สถานะคือ[0, 5]ที่ i=0,[5]ที่ i=0, จากนั้น[5]ที่ i=1. เงื่อนไขสุดท้ายเป็นเท็จ ดังนั้นจึงไม่มีการเข้าถึงข้อมูลนอกขอบเขตของอาร์เรย์
ลูปย้อนกลับปฏิบัติตามกฎที่แตกต่างออกไป
- การวนลูปย้อนกลับเริ่มต้นที่ดัชนีสุดท้ายที่มีอยู่จริง:
for (int i = list.size() - 1; i >= 0; i--)ลบองค์ประกอบที่ตรงกันด้วยremove(i); ค่า i ถัดไปจะเป็นค่าน้อยลงเสมอหนึ่ง - การลบที่ตำแหน่ง i จะทำให้เฉพาะดัชนีถัดไปเลื่อนตำแหน่ง ซึ่งได้ผ่านการตรวจสอบแล้ว องค์ประกอบที่ยังไม่ได้ตรวจสอบด้านหน้าจะคงดัชนีเดิมไว้ ดังนั้นการลดค่า i หลังการลบจึงถูกต้องในกรณีนี้; คำว่า "ห้ามเพิ่มค่าหลังการลบ" ไม่ใช่กฎสากล
Choose an index update that matches the direction of traversal. In the forward while-loop, stay after removal; in the backward for-loop, continue decrementing. Direct structural changes during an enhanced loop are a separate unsafe pattern, even when no fail-fast exception appears.
ใน while-loop แบบไปข้างหน้าที่แสดงไว้นี้, เพิ่ม i ก็ต่อเมื่อคุณ...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
หลัง remove(i), องค์ประกอบถัดไปจะ-shift เข้ามาอยู่ใน i — อย่าข้ามมัน
ลูปแบบ naive แบบ forward บน [0, 0, 5] ที่ลบที่ i=0 แล้วเพิ่ม i จะข้ามศูนย์ถัดไป
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ค่าศูนย์ที่เหลือจะย้ายเข้าสู่ดัชนี 0 แต่การทดสอบถัดไปจะดำเนินการที่ดัชนี 1. การวนลูปแบบย้อนกลับใช้กฎดัชนีที่ถูกต้องแตกต่างกัน
ทางเลือกอื่นของการลบแบบไม่ย้ายตำแหน่งเพื่อหลีกเลี่ยงการเลื่อนข้อมูล คือการ...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Adding keepers to a fresh list sidesteps shifting
ArrayList algorithms reuse array patterns via...
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Swap array [] and length for get(i) and size()
The resizable nature of an ArrayList lets you insert and delete elements, unlike a fixed array
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
add and remove change the list's size
วงลูปลบแบบย้อนกลับที่เริ่มต้นจาก size()-1 ต้องคงอยู่ที่ดัชนีเดิมหลังจากการลบทุกครั้ง
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
ผิด: องค์ประกอบที่ยังไม่ตรวจสอบก่อนหน้าจะยังคงรักษาดัชนีไว้ ดังนั้นวงลูปย้อนกลับจึงลดค่าลงตามปกติ
สร้างลิสต์ใหม่เมื่อต้องรักษาลิสต์เดิมไว้
- To preserve the original list, create a fresh result and append every nonzero element while reading the original. This keeps the original sequence and its size unchanged; it uses additional list storage.
- For lists of mutable objects, copying retained references does not clone those objects. A new list structure may still share its element objects with the original; distinguish list identity from element identity.
การลบแบบ forward จะคงดัชนีหลังจากการลบไว้
ศูนย์สองตัวที่อยู่ติดกันจะถูกตรวจสอบที่ดัชนี 0 ก่อนที่จะรักษา 5 ไว้
ตรวจสอบกรณีที่จะเปิดเผยอัลกอริทึม
- Test an empty list, all zeros, no zeros, adjacent zeros and a zero at the last index. For in-place removal, other references to the same list observe its change; a fresh result leaves that original structure intact.
- An ArrayList can insert or delete elements; those operations can shift later indices. A search may return a found index or -1 when absent; a maximum requires an explicit empty-list policy. Reusing array patterns through get/size helps, but each algorithm still needs its own bounds and contract.
Removing shifts indices. Explain which elements remain unchecked and choose the next index accordingly. Forward stay-or-increment and backward decrement are both valid patterns; a fresh list of keepers preserves the original structure but may share objects.
Order the states for the corrected removal loop on [0, 0, 5].
ความยาก 3/5 · 200 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Each removal rechecks the shifted value; keeping 5 finally advances the index.
วิธี removeZeros แบบไปข้างหน้าที่แสดงอยู่มีการเรียกใช้ get_calls จำนวนเท่าใดเมื่อทำงานกับรายการว่าง?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
เงื่อนไขเริ่มต้น 0 < 0 เป็นเท็จ จึงไม่มีการเรียกใช้ get
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้