Answered step by step
Verified Expert Solution
Question
1 Approved Answer
The purpose of this assignment is to practice using the Java ArrayList class and the methods described below. ArrayList and Random are both in java.util
The purpose of this assignment is to practice using the Java ArrayList class and the methods described below. ArrayList and Random are both in java.util
- Write a Java program that will create an ArrayList of size 10 that holds integers. Use a random number generator to add 8 integers to an ArrayList called list1 so that after each number is added to list1 it is sorted from largest (at index 0)) to smallest. Each number you add to list1 must be placed in the correct spot using a linear search to find the right spot then add the number there. You must use the add(index, element), get(index) and size( ) methods to perform the search and then to place the element. Display the contents of list1 after each number is added to verify that list1 is sorted each time. (This will help you discover errors in your code) You can do this using the toString() method from the ArrayList class.
- Create list2 also with 8 elements sorted from largest to smallest by repeating step #1 above. This time you dont need to print out list2 after each number youve added. Just print it out once after all 8 numbers have been added.
- Remove the numbers from list2, one at a time, using the remove(index) method, and add them to list1 so that list1 is always sorted from largest to smallest. You must keep track of where you added the last number in list1, using a here variable. Add the second number from list2 to list1 by starting at here described above. Continue to move numbers from list2 to list1 in this fashion. Print out list1 and list2 once you are done. Note that list1 will automatically grow bigger to accommodate the additional numbers.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started