I am trying to solve a problem on CodeWars that has me finding the smallest number in an array. Here is my current code:
public class SmallestIntegerFinder {
public int findSmallestInt(int[] args){
int smallest = args[0];
for(int i=1; i < args.length; ++i){
if(args[i] < args[0]){
smallest = args[i];
}
}
return smallest;
}
}
Why doesn't this work? But when I change args[0] in the if statement to the variable smallest it works. What's the difference between the two? They both point to args[0] don't they?
public class SmallestIntegerFinder {
public int findSmallestInt(int[] args){
int smallest = args[0];
for(int i=1; i < args.length; ++i){
if(args[i] < smallest){
smallest = args[i];
}
}
return smallest;
}
}
The test array i'm using is: {78,56,232,12,11,43}