searching an array

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Mar 2007
Posts: 154
Reputation: mrjoli021 is an unknown quantity at this point 
Solved Threads: 0
mrjoli021 mrjoli021 is offline Offline
Junior Poster

searching an array

 
0
  #1
Nov 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2007
Posts: 706
Reputation: stultuske is a jewel in the rough stultuske is a jewel in the rough stultuske is a jewel in the rough 
Solved Threads: 84
stultuske's Avatar
stultuske stultuske is offline Offline
Master Poster

Re: searching an array

 
0
  #2
Nov 15th, 2008
never really used 2d arrays, but when I did (as a student) I took the easy way:

  1. public class SearchChar {
  2.  
  3. public static void main(String args[]){
  4. char toFind = 'a';// determin the char you want;
  5. char[][] array2d = {{'1','k','d'},{'a','p'}};//a given array
  6.  
  7. int a = -1;
  8. int b = -1;
  9. System.out.println("eerste lengte: " + array2d.length);
  10. System.out.println("tweede lengte: " + array2d[0].length);
  11. for ( int i = 0; i < array2d.length; i++){
  12. System.out.println("i = " + i);
  13. for ( int j = 0; j < array2d[i].length; j++){
  14. char test = array2d[i][j];
  15. if (test == toFind){
  16. a = i;
  17. b = j;
  18. }
  19. }
  20. }
  21. System.out.println("a : " + a + " b : " + b);
  22. }
  23.  
  24. }
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC