'Return index if if it exists within the target in an array
I have the following array in which I need to return the index if it exists within the array. I think that I am on the right track however I am confused on how I should return my target index to actually produce what I am trying to return. If the index doesn't exist it should return -1 and if multiple targets are found the first occurrence should be returned. The code that I have currently written is below.
public int getIndex(String target) {
for (int i = 0; i < arr.length;i++) {
for (int j = 0; j < arr[i].length; j++) {
if (arr[i][j] == target) {
return target;
}
}
}
return -1;
}
Any help or tips would be appreciated.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
