I'm working on a java program, but I'm stuck on this specific section. This function is suppose to take an array as input and returns the index of the first occurrence of target in the input array, or -1 if not found. The function is suppose to call my contains method.
contains()
public static boolean contains(int[] input, int target) {
for(int i = 0; i < input.length; i++){
if (target == input[i]){
return true;
}
}
return false;
}
indexof()
public static int indexOf(int[] input, int target) {
if(contains(input, target) == true){
return i;
}
return -1;
}
I am trying to return what the variable i was in the contains method, but I'm not sure how to make the variable i transfer from the contains method to the indexof method without making it a public int in the constructor. Thanks for your help.
----------EDIT---------- The contains method is suppose to search the given array for the target value. If the target value exists somewhere in the array, return true. if not, return false. If any alterations need to be made to the contains method, that may be done as well.