I am trying to find the Minimum and Maximum Value in an ArrayList without Sorting:
Here is my Current Attempt - However I cannot seem to be getting it to work properly:
import java.util.*;
class Untitled {
static ArrayList<Integer> al = new ArrayList<Integer>();
public static void main(String[] args) {
System.out.println(populateArray(al));
System.out.println(findMin(al,0));
}
public static ArrayList<Integer> populateArray(ArrayList<Integer> a) {
al.add(1);
al.add(2);
al.add(30);
al.add(3);
al.add(13);
al.add(34);
al.add(4);
al.add(3);
al.add(2);
al.add(1);
al.add(93);
return a;
}
public static int findMin(ArrayList<Integer> a, int start) {
int min = start;
for(int i=start; i<a.size(); i++) {
if(a.get(i) < a.get(min)) {
min = i;
}
}
return start;
}
}
startat the end.Collections.minandCollections.max. But why?mininstead ofstart, you don't updatestart