I'm studying the search loop for an Array. when I tried to do the code by my own, there were some errors I can not found out.By the way I using Netbeans for java coding.
My array search code:
public class JavaApplication {
public static void main(String[] args) {
int[] nums = new int[5];
nums = new int[]{2, 4, 6, 8, 7};
int target = 7;
for (int i = 0; i < nums.length; i++) {
if (nums[i] == target) {
return i;
}
}
return -1;
}
}
The NetBeans said that there is a unnecessary return statement for my last return statement.
How can I fix the return statement, and run this code?