比较两个2D阵列并将值复制到另一个2D阵列

时间:2014-11-02 17:56:24

标签: java arrays

这是我第一次在这里问一个问题..我不熟悉java,我正在尝试在java中实现一个算法,我必须将两个4rowsX3col 2D数组与存储在其中的字符串进行比较。我必须将第一个数组的第一行与第二个数组的所有行进行比较,依此类推......并且必须将以下格式的不同值复制到另一个4X4 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]]]

我进一步改变了。以下是我的结果。无法得到&#34; +&#34;符号虽然

[[[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]]]

1 个答案:

答案 0 :(得分:0)

可以使用+运算符连接字符串:

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

然后,您可以将结果分配给结果数组的相应元素。

如果你要以增量(例如,循环)拼接一个字符串,使用StringBuilder更有效率,并在结束时通过调用它来创建字符串&& #39; s toString()方法。