Your question can be read two ways. If you want to be able to return just the first index of the array, then do the following:
if(v.length > 0)//check length
return 0;//return first position
return -1;//return empty string meaning there wasnt a first position.
However, you may mean to ask to return the first case of a String s
in the array v
. Then in that case, do the following:
//assuming v and s are not null or do not contain null values
for(int i = 0; i < v.length; i++){//loop through the array v
if(v[i].equals(s){//does the current value of v equal to the String s?
return i;//found string!
}
}
return -1;//didnt find the string
You seem new to java
. I highly recommend reading these two sources: