The following question makes use of the generic Box
class discussed in the reading assignment.
public static class Box<T> { private...
The following question makes use of the generic Box
class discussed in the reading assignment.
public static class Box<T>
{
private T value;
public Box(T val)
{
value = val;
}
public T getValue()
{
return value;
}
}
The method below takes in a list of Box
objects, each containing
an Integer
value, and should return the index of the last box
that contains a negative number. You can use a numeric for loop
to iterate over the list in reverse order. The method should
return -1 if the value is not found anywhere in the list.
For example, consider this list.
List<Box<Integer>> arr = new List<Box<Integer>>();
arr.add(new Box<Integer>( 1)); // Box at index 0, contains 1
arr.add(new Box<Integer>(-2)); // Box at index 1, contains -2
arr.add(new Box<Integer>(-3)); // Box at index 2, contains -3
arr.add(new Box<Integer>( 4)); // Box at index 3, contains 4
A call to lastNegativeBox()
on this list should return 2.
Your feedback will appear here when you check your answer.