Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 比较两个二维数组并将值复制到另一个二维数组_Java_Arrays - Fatal编程技术网

Java 比较两个二维数组并将值复制到另一个二维数组

Java 比较两个二维数组并将值复制到另一个二维数组,java,arrays,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

这是我第一次在这里提问。。我不太熟悉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], [e2],     [c3]], 
 [[b2],         [c3, e2], [e2],     [c3]], 
 [[c1, e1],     [b1, c3], [b1, c1], [b1, c3, e1]], 
 [[b2, c1, e1], [c3],     [c1],     [c3, e1]]]

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

String string1 = "b1";
String string2 = "c3";
String result = string1 + "+" + string2; // result will be the string "b1+c3"
然后可以将结果分配给结果数组的相应元素


如果要以增量(例如,在循环中)拼凑字符串,则更有效的方法是使用a并在末尾通过调用其toString方法创建字符串。

请发布您迄今为止编写的代码。显示一些努力。如何将4x3中的值复制到4x4 2D数组?应该将什么复制到第四列?“这个问题还不清楚。”拉尼菲什说,“从这个例子来看,这个问题似乎很清楚。”。第一列是比较阵列1的第一行和阵列2的每一行的结果;第二列是比较数组1的第二行和数组2的每一行的结果,请详细说明clear@DroidIcs非常清楚。见特德·霍普的评论。
String string1 = "b1";
String string2 = "c3";
String result = string1 + "+" + string2; // result will be the string "b1+c3"