Java ArrayList Exercises, Practice & Solutions
This resource offers a total of 110 Java ArrayList problems for practice. It includes 22 main exercises, each accompanied by solutions, detailed explanations, and four related problems.
[An Editor is available at the bottom of the page to write and execute the scripts.]
ArrayList:
In Java, an ArrayList is a resizable array implementation of the List interface provided by the Java Collections Framework. It's part of the java.util package. Unlike arrays, which have a fixed size, ArrayList can dynamically grow and shrink in size as elements are added or removed. This makes it a more flexible data structure for handling collections of objects.
1. Create and Print ArrayList
Write a Java program to create an array list, add some colors (strings) and print out the collection.
Click me to see the solution
2. Iterate ArrayList Elements
Write a Java program to iterate through all elements in an array list.
Click me to see the solution
3. Insert at First Position
Write a Java program to insert an element into the array list at the first position.
Click me to see the solution
4. Retrieve Element by Index
Write a Java program to retrieve an element (at a specified index) from a given array list.
Click me to see the solution
5. Update ArrayList Element
Write a Java program to update an array element by the given element.
Click me to see the solution
6. Remove Third Element
Write a Java program to remove the third element from an array list.
Click me to see the solution
7. Search Element in ArrayList
Write a Java program to search for an element in an array list.
Click me to see the solution
8. Sort ArrayList
Write a Java program to sort a given array list.
Click me to see the solution
9. Copy ArrayList
Write a Java program to copy one array list into another.
Click me to see the solution
10. Shuffle ArrayList
Write a Java program to shuffle elements in an array list.
Click me to see the solution
11. Reverse ArrayList
Write a Java program to reverse elements in an array list.
Click me to see the solution
12. Extract Sublist from ArrayList
Write a Java program to extract a portion of an array list.
Click me to see the solution
13. Compare Two ArrayLists
Write a Java program to compare two array lists.
Click me to see the solution
14. Swap ArrayList Elements
Write a Java program that swaps two elements in an array list.
Click me to see the solution
15. Join Two ArrayLists
Write a Java program to join two array lists.
Click me to see the solution
16. Clone ArrayList
Write a Java program to clone an array list to another array list.
Click me to see the solution
17. Clear ArrayList
Write a Java program to empty an array list.
Click me to see the solution
18. Check if ArrayList is Empty
Write a Java program to test whether an array list is empty or not.
Click me to see the solution
19. Trim ArrayList Capacity
Write a Java program for trimming the capacity of an array list.
Click me to see the solution
20. Increase ArrayList Capacity
Write a Java program to increase an array list size.
Click me to see the solution
21. Replace Second Element
Write a Java program to replace the second element of an ArrayList with the specified element.
Click me to see the solution
22. Print Elements by Position
Write a Java program to print all the elements of an ArrayList using the elements' position.
Click me to see the solution
Java Practice online
More to Come !
Do not submit any solution of the above exercises at here, if you want to contribute go to the appropriate exercise page.