Question: The ArrayList class creates an array that can resize on demand. Once you learn about ArrayList, there is little reason to use arrays. The container
The ArrayList class creates an array that can resize on demand. Once you learn about ArrayList, there is little reason to use arrays. The container allocates more memory automatically as more elements are added.
Write a test program that does the following:
- Create a new empty ArrayList of String type called testList
- Print the current size of testList.
- Add at least 5 elements to testList.
- Print testList. ArrayList has a toString method so its easy to print.
- Using get(0), print the first element of testList.
- Print the last element of testList. This time, do no use a constant to get the last element, use testList. Size().
- Use a for loop to print each element of testList.
- Use a for-each loop to print each element of testList.
- Use Collections.sort(testList) to sort your ArrayList.
- Use the testList.remove(0) method to remove the first element of the list. remove(0). Put this call inside a println statement so you can observe the return value. Print testList.
- Use the remove method to remove a specific string by its string value. Put this call inside a println statement so you can observe the return value. Print testList.
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
