I'm trying to find the smallest integer in an ArrayList just by using two simple for loops. I tried originally with one for loop, but it wasn't updating correctly. I haven't learned collections yet, so this should be done without using any collections code.
This is what I have:
public static void printInOrder(ArrayList<Integer> data){
int minIndex = 0 ;
for(int i = 0; i < data.size(); i++){
minIndex = i;
for(int j = i + 1; j < data.size() - 1; j++){
if(data.get(j) < data.get(minIndex)){
minIndex = j;}
}
System.out.println(data.get(minIndex) + " ");
}
}//printInOrder
My minimum always seems to be the last value in the list, so I tried printing the data.get(minIndex) in the first for loop to see what happens, and it seems to update it different values, and then finally, the last value. I have no idea why this is happening.
This is what it's printing for example:
Original list:
[47, 19, 46, 42, 15, 26, 36, 27, 13, 15, 1, 40, 34, 14, 6, 34, 28, 12, 15, 13]
Print the minimum:
1 1 1 1 1 1 1 1 1 1 1 6 6 6 6 12 12 12 15 13
ifstatement? Because I tried that way for a while, but it too wasn't giving me the correct minimum.i, for all starting indicesi. You wanted to find the smallest number of the list starting at0only.