Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/332.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将一行中的每个元素与2D数组中的所有元素进行比较_Java_Multidimensional Array - Fatal编程技术网

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

Java将一行中的每个元素与2D数组中的所有元素进行比较,java,multidimensional-array,Java,Multidimensional Array,我有一个2D数组,我正在尝试逐步遍历数组,这样对于第一行,我想逐步遍历每个元素,并将它们与行中的所有其他元素进行比较,以检查我感兴趣的一些条件。然后移到下一行,做同样的操作,并重复,直到我跨过整个数组。我感兴趣的条件在我的if/else块中 以下是我的二维阵列示例: int [][] a = { {4,16,5}, {1,12,1}, {8,9,13}, {3,4,7}}; 这是我的密码: public class ArrayElementComparison { /** * @param

我有一个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");
                }
            }
        }

    }
}

从输出中可以明显看出,它捕获的是前3个条件,而不是第四个条件(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 k=0;i16”);
//类似地构造剩余的“else-if”条件。

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

要回顾该行中的所有元素,您需要添加进一步的内部循环:

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 ...
    }
}
for(int i=0;i对于(int n=1;n您需要在每个子数组中迭代。请尝试以下方法:

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");
            }
        }
    }
}
int[]a={4,16,5},{1,12,1},{8,9,13},{3,4,7};
for(int i=0;i16”);
}else if(当前==12&&comparedTo==1){
System.out.println(“12->1”);
}else if(当前==9&&comparedTo==13){
System.out.println(“9->13”);
}else if(当前==3&&comparedTo==7){
System.out.println(“3->7”);
}
}
}
}

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

在以下代码中,函数
包含的
执行此任务。以3个数字行为例,它将比较索引
0,1
0,2
1,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);
        }
    }
}
公共类数组元素比较{
公共静态布尔包含(int[]a、int n1、int n2){
对于(int i=0;i16”);
}else if(包含(a[i],12,1)){
System.out.println(“12->1”);
}else if(包含(a[i],9,13)){
System.out.println(“9->13”);
}else if(包含(a[i],3,7)){
System.out.println(“3->7”);
}
}
}
私有静态void打印行(int[]行){
for(int i:行){
系统输出打印LN(i);
}
}
}

您需要围绕您的条件添加一个内部循环,该循环将
-1
替换为
-n
,其中
n
是要回顾的元素数。为了让我理解您的问题……您需要将4与16和5进行比较,然后将16与5进行比较,然后在下一行中进行相同的操作吗?@brlaranjeira是的。您是co更正。但解决方案似乎已经发布。感谢大家的帮助!我已经实现了您的建议,并收到以下错误:a[I][k-1]4->16 a[I][k-1]16 a[I][k-1]5线程“main”java.lang.ArrayIndexOutOfBoundsException:3 at ArrayElementComparison.main(ArrayElementComparison.java:20)这真是太棒了!谢谢@CDelaney的帮助。也谢谢大家。通过运行
inner
数组中最内部的循环,使用
for(int n=k+1;n
,可以避免创建数组(
subInner
)的副本。使用
compareTo=inner[n]
第二个值。第二个循环应该是:
for(int k=0;k
。它只需要运行到倒数第二个项,因为它还有一个剩余项要比较。(原始代码也可以工作,但是会使用空的“subInner”数组执行额外的循环。)@乌多博科夫斯基对这两种说法都表示赞同。我选择可读性是为了让事情变得非常清楚。
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);
        }
    }
}