943,907 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 2508
  • Java RSS
Nov 15th, 2008
0

searching an array

Expand Post »
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.
Similar Threads
Reputation Points: 7
Solved Threads: 0
Junior Poster
mrjoli021 is offline Offline
170 posts
since Mar 2007
Nov 15th, 2008
0

Re: searching an array

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

Java Syntax (Toggle Plain Text)
  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. }
Reputation Points: 935
Solved Threads: 356
Nearly a Posting Maven
stultuske is offline Offline
2,497 posts
since Jan 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: need help for clarifications
Next Thread in Java Forum Timeline: Collision Detection - Micro Java





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC