Now it does print 1
class Masi {
public static void main( String [] args ) {
char [] list = { 'm', 'e', 'y' };
// Prints 1
System.out.println( indexOf( 'e', list ) );
}
private static int indexOf( char c , char [] arr ) {
for( int i = 0 ; i < arr.length ; i++ ) {
if( arr[i] == c ) {
return i;
}
}
return -1;
}
}
Bear in mind that
"e"
is an string object literal ( which represents an string object that is )
While
'e'
Is a character literal ( which represents a character primitive datatype )
Even when
list[]
Would be valid Java ( which is not ) comparing the a character element with a string element would return false anyway.
Just use that indexOf string function and you could find any character within any alphabet ( or array of characters )