2013-10-27 4 views
1

Я пытаюсь выяснить, где второй массив дано будет расположен в первом массиве, но не могу показаться, чтобы преобразовать затем в строку, чтобы узнать, где они будут:Сравнение 2D массивы Java

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 < 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; 

     //beginning comparison 
     if (largerString.indexOf(smallString) >= 0) { 

      int temp = largerString.indexOf(smallString); 
     } 

     //debug purposes 
     System.out.println(largerString.indexOf(smallString)); 
     System.out.println(Arrays.toString(array)); 
     System.out.println(Arrays.toString(array1)); 
    } 
    //for debug purposes 
    return null; 
} 
+4

В чем вопрос? – Zong

+0

Где задан массив? – TAAPSogeking

+0

Единственная проблема, которую он выразил, заключается в том, что он не может преобразовать матрицы в строку, чтобы узнать, где находится вторая матрица. Вероятно, он спрашивает, как это сделать. – sdanzig

ответ

0

Попробуйте этот код я только что закончил, он может иметь некоторые ошибки, он будет пытаться улучшить его. Он не использует строки.

private static int[] find(char[][] image, char[][] waldo) { 
    int row = -1; 
    int column = -1; 
    char first_waldo = waldo[0][0]; // first char of waldo 
    boolean continue1 = true; // this is used to avoid looping when the indexes tried are not a possible answer 
    int size = waldo.length * waldo[0].length; // number of elements of waldo 
    int cont = 0; 

    for (int i = 0; i < image.length; i++) { 
     for (int j = 0; j < image[i].length; j++) { // Looping over chars in image array 
      char current = image[i][j]; // Current char of image array 
      if (current == first_waldo) { // If true, this indexes (i, j) are a possible answer 
       row = i; // Current image row 
       column = j; // Current image column 
       cont = 0; // Count of how many chars are matched 
       for (int k = row; k < row + waldo.length; k++) { 
        for (int l = column; l < column + waldo[0].length; l++) { // Looping over 
                 // the possible matching array in image 
//       System.out.println("Comparing " + image[k][l] + " with " + waldo[k - row][l - column]); 
//       System.out.println(k + " " + l); 
         if (waldo[k - row][l - column] == '.') { // If its a point, count as matching characters 
          cont++; 
          continue; 
         } 
         if (image[k][l] != waldo[k - row][l - column]) { // If chars are different, it's not the answer 
          row = -1; 
          column = -1; 
          continue1 = false; // So it doesn't continue looping 
          break; 
         } else { 
          cont++; // If they are equals, count as matching characters 
         } 
        } 
        if (continue1 == false) { // To avoid overlooping when it's not the answer 
         continue1 = true; // Reset value 
         break; 
        } 
       } 
       if (cont == size) { // Is the number of matched charactes equal to the size of waldo? 
        int[] res = {row, column}; 
        return res; // Return indexes 
       } 

      } else { 
       row = -1; 
       column = -1; 
      } 

     } 
    } 
    int[] res = {-1, -1}; 
    //  System.out.println("\n" + row +" , " + column); 
    return res; // Not answer 

} 
0

В I понять задачу - найти подходящую подматрицу. Если вы заменяете точки с помощью подстановочных знаков, это имеет смысл. Таким образом, меньшая матрица находится в позиции (строка = 2, столбец = 8), считая с 1.

Вот код, который ищет подматрицу. Я использовал регулярные выражения для сравнения строк в виде строк:

public static int[] find(char [][] large, char [][] small) { 
    outer: for (int i = 0; i < large.length - small.length; i++) { 

     int column = -1; 

     for (int j = 0; j < small.length ; j++) { 
      String largerString = new String(large[i + j]); 

      String smallString = new String(small[j]); 

      Matcher matcher = Pattern.compile(smallString).matcher(largerString); 

      if(!matcher.find()){ 
       continue outer; 
      } 

      int thisColumn = matcher.start(); 

      if(column != -1 && column != thisColumn){ 
       continue outer; 
      } 

      column = thisColumn; 
     } 

     System.out.printf("found it: %d, %d!", i, column); 
    } 

    //for debug purposes 
    return null; 
} 
Смежные вопросы