繁体   English   中英

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

[英]Java Comparing each element in a row to all elements in a 2D array

我有一个2D数组,我试图逐步遍历该数组,以便对于第一行,我想逐步遍历每个元素并将它们与该行中的所有其他元素进行比较,以检查我感兴趣的某些条件然后移到下一行,执行相同的操作,然后重复直到我逐步遍历整个数组。 我感兴趣的条件在我的if / else块内。

这是我的示例二维数组:

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-> 7)。 我意识到这是因为它仅检查当前元素的下一个相邻元素。 但是,我不知道如何修复代码,以便它检查整行,而不仅仅是下一个相邻的行。

您需要在每个子数组中进行迭代。 试试看:

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");
            }
        }
    }
}

您需要另一级嵌套循环。

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

您的第二个循环需要循环通过比较左侧的所有位置,而新的第三个循环则需要循环进行比较右侧的所有剩余位置。

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.

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

要浏览该行中的所有元素,您需要添加另一个内部循环:

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 ...
    }
}

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

在下面的代码中,该函数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);
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM