Consider an array of integers We need to copy that array int

Consider an array of integers. We need to copy that array into an ArrayList, in reversed order. To do this, you need to complete the method, named copyReverse, in the class named ArrayOps.java. There is only one parameter to this method: the original array of integers. The method returns the new ArrayList, with the contents reversed in order from the original array.

For example, consider the following array of integers:

When you call the copyReverse method with this array, the return value should be a new ArrayList containing the same elements, but in reversed order:

Complete the following code:

Solution

Here is the update for you:

import java.util.ArrayList;

public class ArrayOps
{
/**
This method goes through the array of integers identified by
the only parameter, creating a new ArrayList from the array,
but in reverse order.
@param theArray, an array of integers
@return reversedArr, the new ArrayList of Integers

*/
public static ArrayList copyReverse(int[] anArray)
{
// declare new ArrayList
ArrayList list = new ArrayList();
// your work here

// loop though anArray, in reverse,
// storing each element in the ArrayList
// your work here
for(int i = anArray.length-1; i >= 0; i++)
list.add(anArray[i]);
// return new ArrayList
// your work here
return list;
}
}

Consider an array of integers. We need to copy that array into an ArrayList, in reversed order. To do this, you need to complete the method, named copyReverse,

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site