2014-11-02 6 views
-3

이것은 여기에 질문하는 나의 첫 번째 시간입니다. 나는 자바에 정통하지 않고 두 개의 4rowsX3col을 비교해야하는 java에서 알고리즘을 구현하려고합니다. 문자열이 저장된 2D 배열 첫 번째 배열의 첫 번째 행과 두 번째 배열의 모든 행을 비교해야하며 다른 값은 다른 4X4 2D 배열에 다음 형식으로 복사해야합니다.두 개의 2D 배열을 비교하고 다른 2D 배열에 값을 복사하기

Array1        
b2 c1 e1 
b1 c3 e2 
b1 c1 e2 
b1 c3 e1 

Array2 
b1 c1 e1 
b1 c1 e1 
b2 c2 e2 
b1 c2 e2 

Resultant Array 
b2   c3+e2  e2   c3 
b2   c3+e2  e2   c3 
c1+e1  b1+c3  b1+c1  b1+c3+e1 
b2+c1+e1  c3   c1   c3+e1 

어떻게하면됩니까? 나는 그들을 비교하는 방법을 알고 있지만 어떻게 그들을 추가하고 배열에 복사할지 모르겠다. 제발 도와주세요!

나는 코멘트를보고 난 내 코드를 붙여

import java.util.*; 

public class Divide { 

    // Input dataset 
    String[][] dataset = new String[][] { { "b2", "c1", "e1", "d2" }, 
      { "b1", "c1", "e1", "d1" }, { "b1", "c3", "e2", "d2" }, 
      { "b1", "c1", "e1", "d1" }, { "b1", "c1", "e2", "d2" }, 
      { "b2", "c2", "e2", "d1" }, { "b1", "c3", "e1", "d2" }, 
      { "b1", "c2", "e2", "d1" } }; 

    String[][] dataset1 = new String[4][4]; 
    String[][] dataset2 = new String[4][4]; 

    String[][] discMatrix = new String[4][4]; 

    // Method to divide the dataset in to two matrices based on decision 
    // attribute 
    public void dividedMatrices() { 
     int col = 3, row, i = 1, j = 0, k = 1, m = 0; 
     System.arraycopy(dataset[0], 0, dataset1[0], 0, dataset[0].length); 

     for (row = 1; row < dataset.length; row++) { 
      if (dataset[0][3] == dataset[row][col]) { 
       System.arraycopy(dataset[row], 0, dataset1[k], 0, 
         dataset[row].length); 
       k++; 

      } else { 
       System.arraycopy(dataset[row], 0, dataset2[m], 0, 
         dataset[row].length); 
       m++; 
      } 
     } 
    } 

    // Method to form Discernable matrix 
    public void formDiscernablematrix() { 
     ArrayList<String> temp = new ArrayList<String>(); 
     String a = ""; 
     int x = 0, y = 0; 
     // comparing the two datasets 
     for (int i = 0; i < dataset1.length; i++) { 
      for (int k = 0; k < dataset2.length; k++) { 
       for (int j = 0; j < dataset1.length - 1; j++) { 
        if (dataset1[i][j] != dataset2[k][j]) { 
         temp.add(dataset1[i][j]); 
        } 
       } 
       for (int l = 0; l < temp.size(); l++) { 
        a = a + temp; 
       } 
       // copy a to disc matrix 
       discMatrix[y][x] = a; 
       y++; 
       temp.clear(); 
       a = ""; 
      } 
      x++; 
      y = 0; 
     } 
     System.out.println(Arrays.deepToString(discMatrix)); 
    } 

결과 아래의 결과입니다 : 내가 더 변경

[[[b2],         [c3, e2][c3, e2], [e2],    [c3]], 
[[b2],         [c3, e2][c3, e2], [e2],    [c3]], 
[[c1, e1][c1, e1],      [b1, c3][b1, c3], [b1, c1][b1, c1], [b1, c3, e1][b1, c3, e1][b1, c3, e1]], 
[[b2, c1, e1][b2, c1, e1][b2, c1, e1], [c3],    [c1],    [c3, e1][c3, e1]]] 

. 아래는 내 결과입니다.

[[[b2],   [c3, e2], [e2],  [c3]], 
[[b2],   [c3, e2], [e2],  [c3]], 
[[c1, e1],  [b1, c3], [b1, c1], [b1, c3, e1]], 
[[b2, c1, e1], [c3],  [c1],  [c3, e1]]] 
+2

지금까지 작성한 코드를 게시하십시오. 약간의 노력을 보여주십시오. – turingcomplete

+0

4x3의 값을 4x4 2D 배열로 복사하는 방법은 무엇입니까? 4 번째 열에 무엇을 복사해야합니까? 질문은 불분명하다. – ranifisch

+1

@ranifisch - 예제에서 꽤 분명해 보입니다. 첫 번째 열은 'Array1'의 첫 번째 행과'Array2'의 각 행을 비교 한 결과입니다. 두 번째 열은 'Array1'의 두 번째 행과'Array2'의 각 행을 비교 한 결과입니다. –

답변

0

문자열이 + 연산자를 사용하여 연결 할 수 있습니다 불구하고 "+"기호를 가져올 수 없습니다 :

String string1 = "b1"; 
String string2 = "c3"; 
String result = string1 + "+" + string2; // result will be the string "b1+c3" 

그런 다음 결과 배열의 해당 요소에 결과를 할당 할 수 있습니다.

문자열을 반복 단위로 연결하는 경우 StringBuilder을 사용하고 그 끝에 toString() 메서드를 호출하여 문자열을 만드는 것이 더 효율적입니다.

관련 문제