0

I have a 2d char array and i need to find the position of a specific char in the array. what method can i use.

return Arrays.binarySearch(this.matrixCopy, "S");

does not work it keeps returning -1 when there is an S in the array.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by stultuske
0

never really used 2d arrays, but when I did (as a student) I took the easy way:

public class SearchChar {
    
    public static void main(String args[]){
        char toFind = 'a';// determin the char you want;
char[][] array2d = {{'1','k','d'},{'a','p'}};//a given array

int a = -1;
int b = -1;
System.out.println("eerste lengte: " + array2d.length);
System.out.println("tweede lengte: " + array2d[0].length);
for ( int i = 0; i < array2d.length; i++){
    System.out.println("i = " + i);
  for ( int j = 0; j < array2d[i].length; j++){
      char test = array2d[i][j];
          if (test == toFind){
       a = i;
       b = j;
  }
  }
}
System.out.println("a : " + a + " b : " + b);
    }

}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.