THIS IS A JAVA PROGRAM THAT NEEDS TO BE WRITTEN
Write a recursive method void reverse(ArrayList<Object> obj) that reverses an ArrayList of any type of object. For example, if an ArrayList held 4 strings: "hi", "hello", "howdy", and "greetings" the order would become "greetings", "howdy", "hello", and "hi". Implement a recursive solution by removing the first object, reversing the ArrayList consisting of the remaining Objects, and combining the two. Use the following class ArrayListReverser to write and test your program.
import java.awt.Rectangle; import java.util.ArrayList; public class ArrayListReverser { public static void main(String[] args) { ArrayList<Object> myList = new ArrayList<Object>(); myList.add(new Rectangle(1,1)); myList.add(new Rectangle(2,2)); myList.add(new Rectangle(3,3)); myList.add(new Rectangle(4,4)); for (Object o:myList) System.out.println(o); reverse(myList); System.out.println("\nThe order should now be reversed:"); for (Object o:myList) System.out.println(o); } //your method here }
===============================================
import java.awt.Rectangle;
import java.util.ArrayList;
public class ArrayListReverser {
public static void main(String[] args)
{
ArrayList<Object> myList =
new ArrayList<Object>();
myList.add(new Rectangle(1,
1));
myList.add(new Rectangle(2,
2));
myList.add(new Rectangle(3,
3));
myList.add(new Rectangle(4,
4));
for (Object o : myList)
System.out.println(o);
reverse(myList);
System.out.println("\nThe order
should now be reversed:");
for (Object o : myList)
System.out.println(o);
}
//Method to reverse ArrayList
public static void reverse(ArrayList<Object>
inputArrayList) {
//Check if size is greater than
1
if(inputArrayList.size()>1)
{
//Get
current value
Object
currentValue = inputArrayList.remove(0);
//Calling recursively
reverse(inputArrayList);
//Finally add the current element at each stack to end of the
list
inputArrayList.add(currentValue);
}
}
}
Get Answers For Free
Most questions answered within 1 hours.