Java将行中的每个元素与2D数组中的所有元素进行比较

时间:2015-09-18 16:32:09

标签: java multidimensional-array

我有一个2D数组,我试图逐步完成数组,这样对于第一行,我想逐步浏览每个元素并将它们与行中的所有其他元素进行比较检查我感兴趣的一些条件。然后继续前进到下一行,做同样的事情,然后重复,直到我逐步完成整个数组。我感兴趣的条件在我的if / else块中。

这是我的样本2D数组:

int [][] a = { {4,16,5}, {1,12,1}, {8,9,13}, {3,4,7}};

这是我的代码:

public class ArrayElementComparison {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub
    // define the test array

    int [][] a = { {4,16,5}, {1,12,1}, {8,9,13}, {3,4,7}};

    for (int i = 0; i < a.length; i++) {
        for (int k = 1; k < a.length; k++) {
            System.out.println("a[i][k-1] " + a[i][k-1]);


            if (a[i][k-1] == 4) 
            {
                if (a[i][k] == 16)
                {
                    System.out.println("4->16");
                }
            }
            else if (a[i][k-1] == 12)
            {
                if (a[i][k] == 1)
                {
                    System.out.println("12->1");
                }
            }
            else if (a[i][k-1] == 9)
            {
                if (a[i][k] == 13)
                {
                    System.out.println("9->13");
                }
            }
            else if (a[i][k-1] == 3)
            {
                if (a[i][k] == 7)
                {
                    System.out.println("3->7");
                }
            }
        }

    }
}

}

这是输出:

a[i][k-1] 4
4->16
a[i][k-1] 16
a[i][k-1] 5
a[i][k-1] 1
a[i][k-1] 12
12->1
a[i][k-1] 1
a[i][k-1] 8
a[i][k-1] 9
9->13
a[i][k-1] 13
a[i][k-1] 3
a[i][k-1] 4
a[i][k-1] 7

从输出中可以看出,它正在捕获前3个条件,而不是第4个条件(3-> 7)。我意识到这是因为它只检查与当前相邻的元素。但是,我不知道如何修复代码,以便检查整行,而不仅仅是下一个相邻行。

4 个答案:

答案 0 :(得分:1)

您需要在每个子阵列中进行迭代。试试这个:

int[][] a = { { 4, 16, 5 }, { 1, 12, 1 }, { 8, 9, 13 }, { 3, 4, 7 } };

for (int i = 0; i < a.length; i++) {
    int[] inner = a[i];
    for (int k = 0; k < inner.length; k++) {
        int current = inner[k]; // current value being compared

        // copy the remaining items in the array to a new array for iterating 
        int[] subInner = Arrays.copyOfRange(inner, k + 1, inner.length);

        for (int n = 0; n < subInner.length; n++) {
            int comparedTo = subInner[n]; // current value that "current" is comparing itself to
            System.out.println("array " + (i + 1) + " compare " + current + " to " + comparedTo);

            if (current == 4 && comparedTo == 16) {
                System.out.println("4->16");
            } else if (current == 12 && comparedTo == 1) {
                System.out.println("12->1");
            } else if (current == 9 && comparedTo == 13) {
                System.out.println("9->13");
            } else if (current == 3 && comparedTo == 7) {
                System.out.println("3->7");
            }
        }
    }
}

答案 1 :(得分:0)

您需要其他级别的嵌套循环。

你的第一个循环可以保持不变;它遍历行。

你的第二个循环需要遍历比较左侧行中的所有位置,并且新的第三个循环需要遍历比较右侧行中的所有剩余位置。

for (int k = 0; i < a[i].length - 1; k++) { // Modified second loop
    for (int j = k + 1; j < a[i].length; ++) { // Examine remaining elements in the row

        if (a[i][k] == 4 && a[i][j] == 16) {
            System.out.println("4->16");
        // Construct remaining "else if" conditions similarly.

这将捕获适合您的值的同一行中的任何元素对。

答案 2 :(得分:0)

要回顾行中的所有元素,您需要添加更多内部循环:

for (int i = 0; i < a.length; i++) {
    for (int k = 1; k < a.length; k++) {
        System.out.println("a[i][k-1] " + a[i][k-1]);
        for (int n = 1; n <= k; ++n) {
          if (a[i][k-n] == 4) 
          {
              if (a[i][k] == 16) 
              // ...
          } else if ...
    }
}

答案 3 :(得分:0)

这个想法是有两个嵌套循环来检查这两个数字是否存在于一行中。

在以下代码中,函数contains执行此任务。使用3个数字行的示例,它将比较索引0,10,21,2的项目。

您修改的代码如下所示:

public class ArrayElementComparison {

    public static boolean contains(int[] a, int n1, int n2) {
        for (int i = 0; i < a.length - 1; i++) {
            if (a[i] == n1) {
                for (int j = i + 1; j < a.length; j++) {
                    if (a[j] == n2) {
                        return true;
                    }
                }
            }
        }
        return false;
    }

    public static void main(String[] args) {
        // define the test array
        int[][] a = {{4, 16, 5}, {1, 12, 1}, {8, 9, 13}, {3, 4, 7}};

        for (int i = 0; i < a.length; i++) {
            int[] row = a[i];
            printRow(row);
            if (contains(row, 4, 16)) {
                System.out.println("4->16");
            } else if (contains(a[i], 12, 1)) {
                System.out.println("12->1");
            } else if (contains(a[i], 9, 13)) {
                System.out.println("9->13");
            } else if (contains(a[i], 3, 7)) {
                System.out.println("3->7");
            }
        }
    }

    private static void printRow(int[] row) {
        for (int i : row) {
            System.out.println(i);
        }
    }
}
相关问题