I'm trying to run this code to find the key in the array
If the element is not 5 it is supposed to return a -1
The code runs but doesn't return anything. What am I doing wrong?
public class Search{
public static void main(String[] args){
int[] list = new int [5];
list[0] = 1;
list[1] = 2;
list[2] = 3;
list[3] = 4;
list[4] = 5;
int key = 5;
linearSearch(list, key);
}
public static int linearSearch(int [] list, int key) {
for(int i = 0; i < list.length; i ++){
if (key == list [i])
return i;
}
return -1;
}
}