1. Adding an element to the middle (or at the beginning) of a list
If we want to insert an element in the middle of the list, here's what happens inside the list.
Suppose we have a list of 11 elements:
We want to insert the number 10,000 into the list at index 5. To do this, we just need to execute:
list.add(5, 10000);
Where list
is an ArrayList
variable. The add(int index, type value)
method adds the value 10000 at position 5 in the list. Here's what happens in the add()
method:
Step 1: All elements of the array, starting from the 5th position, will be shifted (copied) by 1 element toward the end of the array:
Note that the elements in the 5th and 6th cell of the array are now the same.
Step 2: Write the value 10,000 into the 5th cell:
Now the list contains all the elements of the array, and the fifth cell contains the number 10,000. Just what we wanted.
2. Removing an element from a list
Removing an element from a list is similar to inserting it, but the process is reversed.
Let's remove the 3rd element from a list. To do this, we need to execute:
list.remove(3);
Here's what happens in the remove() method:
Step 1: The elements of the array, starting from the 4th position, will be shifted (copied) by 1 element toward the start of the array:
Step 2: The value of the size
variable will be decreased by 1.
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
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 |
---|---|
|
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 |
---|---|
|
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 |
---|---|
|
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 |
---|---|
|
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 |
---|---|
|
|
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.
GO TO FULL VERSION