How do you seach for a string in a 2 dimentional array?
Author |
Message |
rss14
|
Posted: Sat Jun 06, 2009 11:48 am Post subject: How do you seach for a string in a 2 dimentional array? |
|
|
Lets say I want to find the letter "A" in a 2 dimensional array, how can I find what row and column the letter is in?
Here is the code:
Java: | import java.util.Scanner;
public class GPS
{
// instance variables - replace the example below with your own
private String userInput;
private int colPosition, colWaypoint, rowPosition, rowWaypoint, change, displacement;
private String [][] keypad;
Scanner input = new Scanner (System. in);
public GPS ()
{
// initialise instance variables
keypad = new String [5][6];
}
public void keypad ()
{
// put your code here
keypad [0][0] = "A";
keypad [0][1] = "B";
keypad [0][2] = "C";
keypad [0][3] = "D";
keypad [0][4] = "E";
keypad [0][5] = "F";
keypad [1][0] = "G";
keypad [1][1] = "H";
keypad [1][2] = "I";
keypad [1][3] = "J";
keypad [1][4] = "K";
keypad [1][5] = "L";
keypad [2][0] = "M";
keypad [2][1] = "N";
keypad [2][2] = "O";
keypad [2][3] = "P";
keypad [2][4] = "Q";
keypad [2][5] = "R";
keypad [3][0] = "S";
keypad [3][1] = "T";
keypad [3][2] = "U";
keypad [3][3] = "V";
keypad [3][4] = "W";
keypad [3][5] = "X";
keypad [4][0] = "Y";
keypad [4][1] = "Z";
keypad [4][2] = "space";
keypad [4][3] = "-";
keypad [4][4] = ".";
keypad [4][5] = "Enter";
}
} |
Mod Edit: Remember to use syntax tags! Thanks code: | [syntax="java"]Code Here[/syntax] |
|
|
|
|
|
|
Sponsor Sponsor
|
|
|
Zren
|
Posted: Sat Jun 06, 2009 12:57 pm Post subject: Re: How do you seach for a string in a 2 dimentional array? |
|
|
how would you search a one-dimensional array? Think of it as a row.
When thinking of a two-dimensional array, look as it as a bunch of rows. Except now you have two variables. So you search each column in the first row, then move to the next row. Search each column in the second row...etc.
code: | for rows
for columns
compare
end for
end for |
Though your kinda looking at a three demensional array if your using Strings instead of characters, but I'll overlook that for now. |
|
|
|
|
|
rss14
|
Posted: Sat Jun 06, 2009 9:20 pm Post subject: Re: How do you seach for a string in a 2 dimentional array? |
|
|
Yeah I tried that, but it just keeps looping over and over. . .
Can you post some sample code? |
|
|
|
|
|
Zren
|
Posted: Sun Jun 07, 2009 1:49 am Post subject: Re: How do you seach for a string in a 2 dimentional array? |
|
|
Weeeeeeeeeeell sure. Can you post your code if your still not getting it though...since your using counted loops, it shouldn't be going into an infinite loop.
Btw. Since your using Strings. When your comparing the two, make sure its:
string1.contentEquals( string2 ) == true
and not
string1 == string2
...otherwise it will look for if the string of data is at the same position in memory, instead of the two strings with the same content.
Java: |
public class Search2DArray {
public static final int size = 10;
public static void search ( int[][] a, int row, int col, int key ) {
for (int y= 0; y<row; y++ ) {
for (int x= 0; x<col; x++ ) {
if ( a [x ][y ] == key ) {
System. out. println( key + " was found at (" + x + "," + y + ")!" );
return;
}
}
}
//In case it wasn't found
System. out. println("Search term wasn't found.");
}
public static void main (String[] args ) {
int[][] a = new int[size ][size ];
for (int y= 0; y<size; y++ ) {
for (int x= 0; x<size; x++ ) {
a [x ][y ] = 0;
}
}
a [ (int)(Math. random()*size ) ][ (int)(Math. random()*size ) ] = 1;
search ( a, size, size, 1 );
}
}
|
|
|
|
|
|
|
|
|