3 min read•Last Updated on June 18, 2024
Avanish Gupta
Milo Chang
Avanish Gupta
Milo Chang
Now that we have made our ArrayList, it is time to traverse the ArrayList! To traverse an ArrayList, we can use the same two methods that we use to traverse regular arrays: using the regular for loop and the enhanced for loop.
There are only two differences in the for loop implementation.
Here is an example of the for loop implementation:
public static void forTraversal(ArrayList
The enhanced for loop code is basically unchanged. Here is an example of the enhanced for loop implementation:
public static void enhancedForTraversal(ArrayList
As with arrays, we can only change the items in an ArrayList using set(), and not by changing the enhanced for loop variable, as Java is a pass-by-value language. However, as before, we can change instance variables of objects inside ArrayLists using an enhanced for loop.
Sometimes we want to remove elements while traversing the ArrayList. This can only be done with the regular for loop. Changing the size of an ArrayList (either by adding or removing elements) while using an enhanced for loop will result in a ConcurrentModificationException.
When deleting an element at an index i, remember that the element that used to be at index i+1 (the next element) is now at index i. This will require us to do an i-- before the loop incrementation to avoid skipping an element.
The size() in the loop condition will automatically change as you add or remove items, so we don't need to worry about it not being accurate or the method crashing. Here is a method that removes all even numbers from an ArrayList like integerList above:
/** Removes all even numbers
*/
public static ArrayList
Other times, we may want to add elements to an ArrayList during traversal. However, this brings up its own problem. Let's take a look:
/** Duplicates all odd numbers
*/
public static ArrayList
The add() method is used to insert an element into a data structure, such as a list or set.
Term 1 of 10
The add() method is used to insert an element into a data structure, such as a list or set.
Term 1 of 10
The add() method is used to insert an element into a data structure, such as a list or set.
Term 1 of 10
ArrayList is a dynamic data structure that allows you to store and manipulate collections of objects. Unlike arrays, ArrayLists can grow or shrink dynamically as needed.
List Interface: A Java interface that defines common methods for working with lists.
add(): A method used to insert elements into an ArrayList.
remove(): A method used to delete elements from an ArrayList.
A for loop is a control flow statement that allows you to repeatedly execute a block of code for a specified number of times or until certain conditions are met.
While Loop: A while loop repeatedly executes a block of code as long as a given condition remains true.
Loop Variable: The loop variable is a variable that keeps track of the current iteration or count in a loop.
Nested Loop: A nested loop is a loop inside another loop. It allows you to perform more complex repetitive tasks by iterating over multiple sets of data simultaneously.
An enhanced for loop (also known as a foreach loop) is a simplified way to iterate over elements in an array or collection. It automatically handles indexing and provides an easy way to access each element without explicitly using indices.
Array: An array is a data structure that stores multiple values of the same type in contiguous memory locations. Elements in an array can be accessed using their index position.
Iterator: An iterator is an object that allows traversal through elements in a collection one by one. It provides methods like hasNext() and next() to check if there are more elements and retrieve them respectively.
Index: An index is a numerical value that represents the position of an element in an ordered list or data structure. It starts from 0 for the first element and increments by 1 for each subsequent element.
The get() method is used to retrieve the value of an element at a specific index in a list or array.
size(): The size() method is used to determine the total number of elements present in a collection, such as a list or array.
set(): The set() method is used to update or replace the value of an element at a specific index in a list or array.
indexOf(): This method returns the index of the first occurrence of a specified element in a list or array.
The size() method is used to determine the total number of elements present in a collection, such as a list or array.
isEmpty(): This method checks if a collection is empty and returns true if it is, false otherwise.
add(): The add() method adds an element to the end of a collection and increases its size by one.
remove(): This method removes an element from a collection and decreases its size by one.
IndexOutOfBoundsException is an exception that occurs when trying to access an invalid index position within an array or collection.
ArrayIndexOutOfBoundsException: This exception specifically occurs when trying to access an invalid index position within an array.
ArrayList: An ArrayList is similar to an array but can dynamically grow and shrink in size as needed.
LinkedList: A LinkedList is another type of data structure that stores elements in a sequence and allows efficient insertion and deletion at any position.
A set is a collection of unique elements with no specific order. It does not allow duplicate values.
add(): This method adds an element to the set if it is not already present.
contains(): This method checks if a specific element is present in the set.
size(): This method returns the number of elements in the set.
A ConcurrentModificationException occurs when a collection is modified while being iterated over using an iterator or enhanced for loop.
Iterator: An iterator allows us to traverse through a collection and perform operations on its elements.
Enhanced for loop (for-each loop): A simplified way to iterate over collections or arrays without explicitly using an iterator.
Synchronization: The process of controlling access to shared resources in order to avoid conflicts between multiple threads.