1. Thêm phần tử vào giữa (hoặc đầu) danh sách

Nếu chúng ta muốn chèn một phần tử vào giữa danh sách, đây là những gì xảy ra bên trong danh sách.

Giả sử chúng ta có một danh sách gồm 11 phần tử:

Thêm phần tử vào giữa (hoặc đầu) danh sách

Chúng tôi muốn chèn số 10.000 vào danh sách ở chỉ số 5. ​​Để làm điều này, chúng tôi chỉ cần thực hiện:

list.add(5, 10000);

Đâu listlà một ArrayListbiến. Phương add(int index, type value)thức thêm giá trị 10000 ở vị trí 5 trong danh sách. Đây là những gì xảy ra trong add()phương pháp:

Bước 1: Tất cả các phần tử của mảng bắt đầu từ vị trí thứ 5 sẽ bị dịch chuyển (sao chép) 1 phần tử về cuối mảng:

Thêm phần tử vào giữa (hoặc đầu) danh sách 1

Lưu ý rằng các phần tử trong ô thứ 5 và thứ 6 của mảng bây giờ giống nhau.

Bước 2: Viết giá trị 10.000 vào ô thứ 5:

Thêm phần tử vào giữa (hoặc đầu) danh sách 2

Bây giờ danh sách chứa tất cả các phần tử của mảng và ô thứ năm chứa số 10.000. Chỉ là những gì chúng tôi muốn.



2. Xóa phần tử khỏi danh sách

Việc xóa một phần tử khỏi danh sách cũng tương tự như việc chèn phần tử đó, nhưng quá trình này bị đảo ngược.

Hãy xóa phần tử thứ 3 khỏi danh sách. Để làm điều này, chúng ta cần thực hiện:

list.remove(3);

Đây là những gì xảy ra trong phương thức remove():

Bước 1: Các phần tử của mảng bắt đầu từ vị trí thứ 4 sẽ dịch chuyển (sao chép) 1 phần tử về phía đầu mảng:

Xóa một phần tử khỏi danh sách

Bước 2: Giá trị của sizebiến sẽ giảm đi 1.

Xóa một phần tử khỏi danh sách 2

Please note there are some values colored gray at the end of the array. Technically, these are garbage. They need to be removed in order not to interfere with garbage collection.

Step 3: Cleaning up garbage

Xóa một phần tử khỏi danh sách 3



3. Practical examples of working with a list in Java

Let's write some examples of working with lists:

We'll list all the even numbers in the range from 1 to 20:

Code Notes
ArrayList<Integer> list = new ArrayList<Integer>();

for (int i = 1; i <= 20; i++)
   if (i%2 == 0)
      list.add(i);
Create a list object

Loop over all the indices 1 to 20
If the index is divisible by 2 without a remainder,
add it to the list

Now let's display all the elements of the list on the screen:

Code Notes
ArrayList<Integer> list = new ArrayList<Integer>();

for (int i = 1; i <= 20; i++)
   if (i%2 == 0)
      list.add(i);

for (int i = 0; i < list.size(); i++)
   System.out.println(list.get(i));
Create a list object
Loop over all the indices 1 to 20

If the number is divisible by 2 without a remainder,
add it to the list

Loop from zero to the size of the list
Display each element on the screen

Removing elements:

Now let's remove all elements that are divisible by 4. Note that after removing an element from the list, the positions of the remaining elements immediately change.

Code Notes
ArrayList<Integer> list = new ArrayList<Integer>();

for (int i = 1; i <= 20; i++)
   if (i%2 == 0)
      list.add(i);

for (int i = 0; i < list.size(); i++)
   if (list.get(i)%4 == 0)
   {
      list.remove(i);
      i--;
   }
Create a list object

Loop over all the indices 1 to 20
If the index is divisible by 2 without a remainder,
add it to the list

Loop from zero to the size of the list
If an element of the list is divisible by 4 without a remainder:

a) remove the element
b) decrease the i counter so we get the same index on the next iteration of the loop

Let's say you need to remove the last 3 elements from a list.

Here's how to do that incorrectly:

Code Notes
ArrayList<Integer> list = new ArrayList<Integer>();

for (int i = 1; i <= 20; i++)
   if (i%2 == 0)
      list.add(i);

int n = list.size();
list.remove(n - 3);
list.remove(n - 2);
list.remove(n - 1);
Create a list object

The list has 10 elements: 2, 4, 6, ... 20



n = 10
n - 3 = 7 (there are 9 elements left in the list)
n - 2 = 8 (there are 8 elements left in the list)
n - 1 = 9 (there are 7 elements left in the list)

After removing the 7th and 8th elements, only 8 elements will remain in the list. That means it won't be possible to delete the 9th element — an error will occur in the program.

Here's the right way to remove the elements:

Option 1 Option 2
int n = list.size();
list.remove(n - 3);
list.remove(n - 3);
list.remove(n - 3);
int n = list.size();
list.remove(n - 1);
list.remove(n - 2);
list.remove(n - 3);

The elements need to be removed either from the end or from another consistent location, since the elements get shifted by one after each removal operation.