简体   繁体   English

arraylist整数中出现意外结果-pde

[英]Unexpected result in arraylist integer - pde

I write a function that sums up the elements of arrays in a 2d list and add them in a 1d list: 我编写了一个将2d列表中的数组元素求和并添加到1d列表中的函数:

  ArrayList<Integer> sumList(ArrayList<int[][]> list,int side){
    ArrayList<Integer> sums= new ArrayList<Integer>();
    for(int[][] array : list){
    int arraySum = 0;
    //arraySum += CalcMove(array,side);
      for(int i = 0; i < 8; i++){
          for(int j = 0; j < 8; j++){
            arraySum+=array[i][j];
          }
      }
      sums.add(arraySum);
      //println(arraySum);
    }
    println(sums);
    //sums.add(0);
    //println(sums);
    return sums;
  }

The problem is, the result are totally irrelevant,ambiguous and not even in a list. 问题是,结果完全不相关,模棱两可,甚至没有列出。 The println result: println结果:

[-232, -232, -232] -232 [-232, -232, -232] -232 [-223, -223, -223] -223 [-223, -223, -223, -223] -223 [-219, -219, -219] -219 [-214, -214, -214] -214 [-14, -14, -14] -14 [-10, -10, -10] -10 [-5, -5, -5] -5 [-4, -4, -4, -4] -4 [-4, -4, -4, -4] -4 [-4, -4, -4, -4] -4 [-4, -4, -4, -4] -4 [-4, -4, -4, -4] -4 [-4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4, -4] -4 [] [-232,-232,-232] -232 [-232,-232,-232] -232 [-223,-223,-223] -223 [-223,-223,-223,-223]- 223 [-219,-219,-219] -219 [-214,-214,-214] -214 [-14,-14,-14] -14 [-10,-10,-10] -10 [ -5,-5,-5] -5 [-4,-4,-4,-4] -4 [-4,-4,-4,-4] -4 [-4,-4,-4 ,-4] -4 [-4,-4,-4,-4] -4 [-4,-4,-4,-4] -4 [-4,-4,-4,-4,- 4,-4,-4,-4,-4,-4,-4,-4,-4,-4] -4 []

But when I add another element outside the loops (in this case it's zero), the result will be OK (I'm still not sure it is). 但是,当我在循环外添加另一个元素(在这种情况下,它为零)时,结果将是确定的(我仍然不确定是否是这样)。 the println result after this: 之后的println结果:

[-232, -232, -232, 0] [-232,-232,-232、0]

I'm totally confused about this since I'm pretty sure the function does not have any problem cause I tested individually and it's worked just fine. 我对此完全感到困惑,因为我很确定该函数没有任何问题,因为我进行了单独测试,并且一切正常。 And it couldn't be the arraylist cause I wrote the entire project in c# and it's worked just fine, too. 它不可能是arraylist,因为我用c#编写了整个项目,并且也很好用。

Here is a method that collects all integer in a 2d array and put them into a list. 这是一种将2d数组中的所有整数收集并放入列表中的方法。

    public static List<Integer> listElements(int[][] array) {
        List<Integer> list = new ArrayList<Integer>();
        for (int[] subArray : array) {
            IntStream.of(subArray).forEach(i -> list.add(i));
        }
        return list;
    }

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

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