Note that there are some explanatory texts on larger screens.

plurals
  1. POComparing 2 2D char arrays in Java
    primarykey
    data
    text
    <p>I have two 2D char arrays a "large" and a "small" and want to find where the smaller one is located in the larger one.</p> <p>Example: </p> <p>the larger is </p> <pre><code>jfbsvdfaakjdfb dsvkfvwaaksghd aadkfghsgkdldl aakdfghfgbkjbb fdkbaadfkgaskj </code></pre> <p>and the smaller is</p> <pre><code>aa aa </code></pre> <p>It would be located at [0, 7] and [2,0]</p> <p>I'm wondering what would be the easiest way to compare the two? Right now, I'm thinking of for loops which at first compare the first elements of the array and if they don't match go until there is a match then once there is, more for loops which compare each element to see if they match. The other option was setting up HashMaps, but I'm not certain how that would be set up or use them to get results. </p> <p>Upon tinkering with one of the suggestions, I came up with this code: public static int[] findWaldo(char [][] large, char [][] small) {</p> <pre><code> public static void main(String[] args) { char i [][] = new char[][]{ {'d', 's', 'l', 'e', 'i', 'g', 'h', 'e', 'i', 'j', 'a', 's', 'l', 'd', 'k', 'j'}, {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'W', 'w', 'Z', 'Z', 'Z', 'W', '1', 'l', 'k'}, {'h', 'i', 'j', 'k', 'l', 'm', 'n', 'Z', 'A', 'a', 'Z', 'a', 'Z', '2', 'i', 'n'}, {'o', 'p', 'q', 'r', 's', 't', 'u', 'Z', 'Z', 'L', 'l', 'Z', 'Z', '3', 'i', 'v'}, {'v', 'w', 'x', 'y', 'z', '1', '2', 'Z', 'd', 'Z', 'D', 'd', 'Z', '4', 'q', 'i'}, {'3', '4', '5', '6', '7', '8', '9', 'o', 'Z', 'Z', 'o', 'O', 'Z', '5', 'b', 'v'}, {'k', 'e', '8', '7', '8', '4', 'j', 'f', 'l', 'k', 'a', 'v', '8', '8', 'h', 'j'} }; char w [][] = new char[][]{ {'W', 'w', '.', '.', '.', 'W', '1'}, {'.', 'A', 'a', '.', 'a', '.', '2'}, {'.', '.', 'L', 'l', '.', '.', '3'}, {'.', 'd', '.', 'D', 'd', '.', '4'}, {'o', '.', '.', 'o', 'O', '.', '5'} }; find(i, w); } public static int[] find(char [][] image, char [][] waldo) { for (int i = 0; i &lt; waldo.length; i++) { char[] largerCharArray= large[i]; String largerString = new String(largerCharArray); //used for debug purposes char[] array = largerCharArray; char [] smallCharArray = small[i]; String smallString = new String(smallCharArray); char[] array1 = smallCharArray; System.out.println(largerString.indexOf(smallString)); System.out.println(Arrays.toString(array)); System.out.println(Arrays.toString(array1)); } return null; } </code></pre> <p>All this code does is print the first part of the larger array, is there a reason for this?(Please ignore the .'s in the small array because those will then represent any char so it's essentially a match.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload