将一个值与二维数组的其余部分进行比较(Java)

将一个值与二维数组的其余部分进行比较(Java),java,Java,其思想是将元素x[0][0]与数组的所有其他后续元素进行比较,然后将x[0][1]与所有后续元素进行比较,依此类推。。。我的尝试: outerloop: for (int i = 0;i < newBoard.length ;i++){ for (int j = 0; j < newBoard.length;j++){ if (i*j == 9){ System.out.println("BREAK L

其思想是将元素
x[0][0]
与数组的所有其他后续元素进行比较,然后将
x[0][1]
与所有后续元素进行比较,依此类推。。。我的尝试:

  outerloop:
      for (int i = 0;i < newBoard.length ;i++){
         for (int j = 0; j < newBoard.length;j++){
           if (i*j == 9){
             System.out.println("BREAK LOOP");
             break outerloop;
           }
           int actualNumber = newBoard[i][j];
            for (int k = i; k < newBoard.length;k++){
              for (int l = j; l < newBoard.length; l++){
                if (actualNumber > newBoard[k][l]){
                  // do stuff
                }
              }
            }
         }
      }
outerloop:
对于(int i=0;i新板[k][l]){
//做事
}
}
}
}
}
newBoard
只是一个
int[][]
对象。上面的代码不起作用,因为在outerloop的第一次迭代中,我正确地得到了它,但在第二次迭代中,l应该设置为
0
(在这种情况下,第一次迭代将不起作用,因为它将与之前不需要的元素进行比较)


编辑:也许我解释错了,所以我要写一个“完整”的例子来说明我想要实现的目标:x[0][0]应该与x[0][1]、x[0][2]、x[1][0]、x[1][1]、x[1][2]、x[2][2]进行比较(假设使用3x3数组)。在下一次迭代中,x[0][1]应与x[0][2]、x[1][0]、x[1][1]、x[1][2]、x[2][0]、x[2][1]、x[2][2]等进行比较

此代码来自我。代码的问题是,如果内部循环不是正方形,那么它应该取外部数组的长度。如果你有一些问题或没有帮助,请留下评论

public static void main(String[] args) {

    // Assume there's a 3 x 3 array.
    int[][] intArray = new int[][]{
      { 100, 50, 40},
      { 30, 60, 20},
      { 10, 5, 0}
    };

    // Outer loop.
    for (int i = 0; i < intArray.length; i++) {
        // Inner loop.
        for (int j = 0; j < intArray[i].length; j++) {
            // Second round of loop.
            // Compare this element to all the next elements.
            // If you want to compare this with all the elements, set int k = 0.
            for (int k = i; k < intArray.length; k++) {
                for (int l = 0; l < intArray[k].length; l++) {
                    if (intArray[i][j] < intArray[k][l]) {
                        // Do something.
                    }
                }
             }
        }
    }
}
publicstaticvoidmain(字符串[]args){
//假设有一个3x3数组。
int[][]intArray=新int[][]{
{ 100, 50, 40},
{ 30, 60, 20},
{ 10, 5, 0}
};
//外环。
for(int i=0;i
您的代码有3个问题

  • 若您的输入二维数组不是正方形,那个么二级循环可能会抛出索引越界异常
  • 不需要进行i*j==9检查,因为1级和2级循环将考虑退出条件
  • 在3级和4级循环中,您将当前元素与冗余元素本身进行比较 尝试下面的代码,它会工作

    for (int i = 0;i < newBoard.length ;i++){
      for (int j = 0; j < newBoard[i].length;j++){
        int actualNumber = newBoard[i][j]; 
        for (int k = i; k < newBoard.length;k++){
        int l=0;
        if(k==i){
         l=j+1;
        }
        for (; l < newBoard[k].length; l++){ 
            if (actualNumber > newBoard[k][l]){ 
                // do stuff 
            }
          }
        } 
      } 
    }
    
    for(int i=0;i新板[k][l]){
    //做事
    }
    }
    } 
    } 
    }
    
    建议使用简短版本的逻辑。下面是2d int数组的工作代码。 下面的代码是当每行中的数据只需要与该行中其他索引中的剩余数据进行比较时的代码

        int[][] a =
            {{9, 11, 5, 7, 6, 2, 5, 7, 10}, {99, 3344, 233, 44, 667, 777, 888, 777, 45}, {9, 7, 8, 4, 8, 2, 9, 10, 7}};
    
        for (int[] aRowIn2dArray : a) {
            int index = 1;
            for (int currentDataValue : aRowIn2dArray) {
                if (index == aRowIn2dArray.length) {
                    break;
                }
                int nextDataValue = aRowIn2dArray[index++];
                System.out.println(currentDataValue + ", " + nextDataValue);
                if (currentDataValue > nextDataValue) {
                    System.out
                        .println("Found " + currentDataValue + " to be greater than next element " + nextDataValue);
                }
            }
        }
    
    如果需要迭代查找每个索引中的数据是否大于该行和所有后续行中的任何数据,下面是基于Java8的工作代码。将整个数据转换为列表,然后通过子列表迭代检查该值是否大于其他索引中的所有剩余值

    // test data
            Integer[][] testData =
                {{9, 11, 5, 7, 6, 2, 5, 7, 10}, {99, 3344, 233, 44, 667, 777, 888, 777, 45}, {9, 7, 8, 4, 8, 2, 9, 10, 7}};
    
            // list of all data
            List<Integer> listOfAllData = new ArrayList<>();
            for (Integer[] aRowIn2dArray : testData) {
                listOfAllData.addAll(Arrays.asList(aRowIn2dArray));
            }
    
            int column = 1;
            for (Integer[] aRowIn2dArray : testData) {
                for (int currentDataValue : aRowIn2dArray) {
                    // list to search on which is sub-listed to contain forward data only
                    List<Integer> searchableList = listOfAllData.subList((column), listOfAllData.size());
    
                    //see if this value is greater than all values in the search list
                    boolean isCurrentDataValueGreaterThanRest =
                        isDataGreaterThanAllDataInList(currentDataValue, searchableList);
    
                    if (isCurrentDataValueGreaterThanRest) {
                        System.out.println("currentDataValue " + currentDataValue
                            + " is greater than remaining forward data " + isCurrentDataValueGreaterThanRest);
                    }
                    column++;
                }
    
            }
    
    //测试数据
    整数[][]测试数据=
    {{9, 11, 5, 7, 6, 2, 5, 7, 10}, {99, 3344, 233, 44, 667, 777, 888, 777, 45}, {9, 7, 8, 4, 8, 2, 9, 10, 7}};
    //所有数据的列表
    List listOfAllData=new ArrayList();
    for(整数[]arow2darray:testData){
    addAll(Arrays.asList(aRowIn2dArray));
    }
    int列=1;
    for(整数[]arow2darray:testData){
    for(int currentDataValue:aRowIn2dArray){
    //要搜索的列表,其子列表仅包含转发数据
    List searchableList=listOfAllData.subList((列),listOfAllData.size());
    //查看此值是否大于搜索列表中的所有值
    布尔值IsCurrentDataValuer大于REST=
    isDataGreaterThanAllDataInList(当前数据值,可搜索列表);
    如果(IsCurrentDataValuer大于REST){
    System.out.println(“currentDataValue”+currentDataValue
    +“大于剩余的正向数据”+IsCurrentDataValuer大于Rest);
    }
    列++;
    }
    }
    
    方法检查数据是否大于请求列表中的所有值

    private boolean isDataGreaterThanAllDataInList(int data, List<Integer> listOfData)
        {
            Optional<Integer> result = listOfData.stream().filter(obj -> (obj > data)).findFirst();
            return !result.isPresent();
        }
    
    private boolean isDataGreaterThanAllDataInList(整型数据,列表数据)
    {
    可选结果=listOfData.stream().filter(obj->(obj>data)).findFirst();
    return!result.isPresent();
    }
    
    这不起作用。假设在比较4x4数组的第二行第一列时出现这种情况。在这种情况下,对于外环,我们有i=0,j=1。因此,如果以l=j+1开始内部循环(k和l),j永远不会是0!这意味着它将永远不会与第三行、第一列(其中k=2,l应为0)进行比较。在您的查询中,您特别提到n我想知道是否大于下一个元素“”--这是什么意思?我不确定您是否清楚数组索引的概念。这里,
    i&k
    表示行,
    j&l
    表示列。所以,当你在第二排和第一排的时候