Hi I'm having a problem to write a loop which finds the largest value.
A list of integers and an integer variable are declared like this: List list; int max;
Assuming that some values (including negatives!) have been added to the list. i need to find the largest value in list and stores it in max.
// start of test code
import java.util.*;
public class ListTest
{
private List<Integer> list;
private int max;
public void findMaxInListTest()
{
list = new ArrayList<Integer>(); // could be a Linked list
list.add(0);
list.add(-99);
list.add(99);
list.add(1320);
list.add(470000);
list.add(12);
list.add(-5000);
list.add(300);
/*# max = starting value
iterate through list to set max = max value in list
in this case 470000
*/
}
}
I tried using this loop but then encounter an error:
int max = list[0];
for(int i=470000;i < list.length;i++){
if(list[i] > max){
max = list[i];
}
}
return max;
do you know which kind of loop that appropriate for this? Thanks