Array Iteration to Find Maximum
Consider the following Storage class definition.
public class Storage {
private int[] items;
public Storage(int size) {
items = new int[size];
}
}
The following code segment is intended to find the maximum value in the items array of a Storage object. Assume the array has been populated with values.
Storage store = new Storage(10); // assume values are already populated
int maxItem = store.items[0];
for (int item : store.items) {
/* missing code */
}
Which of the following can be substituted for /* missing code */ so that the code segment works as intended?
A
if (store.items[item] > maxItem) {
maxItem = store.items[item];
}
B
if (item > store.maxItem) {
store.maxItem = item;
}
C
if (item > maxItem) {
maxItem = item;
}
D
if (store.items[maxItem] < item) {
maxItem = item;
}
Question Leaderboard
Not enough data yet to show leaderboard.
