简体   繁体   中英

java: comparing value of 2D-array with value of arraylist at the same column

I have a 2D-Array with a lot of zeros and some other values. In the first step I am adding up all rows in one column to find out the sum of each column. The result of the sum is saved in an arraylist. Now I need to compare each value in the 2D-Array if it is equal to the sum of its column in the arraylist. If it is equal the first value of the row will be saved in a seperate arraylist.

As an example:

          {A, 0, 0, 6, 0}
          {B, 0, 2, 0, 2}
          {C, 0, 1, 0, 0}
          {D, 3, 0, 0, 2}

Arraylist    {3, 3, 6, 4}

I want to compare each value with the value in the arraylist at the same position.

My code so far:

List sumList = new ArrayList();
for(int c = 1; c < excelMatrix.length; c++){
    int sum = 0;
    for(int r = 0; r < excelMatrix.length-1; r++){
        sum = excelMatrix[r][c] + sum;
        sumList.add(sum);
    }
}

List checkList = new ArrayList();
for(int c = 1; c < excelMatrix.length; c++){
    for(int r = 0; r < excelMatrix.length-1; r++){
        if(excelMatrix[r][c].equals(sumList)){
            checkList.add(excelMatrix[r][0]);
        }
    }
}

Everything works fine until the comparision of the array value with the value of the arraylist.

You are comparing an integer with the list, not with the correspondent sum of column value in list.

If excelMatrix is Integer array, do this way:

if(excelMatrix[r][c].equals(sumList.get(c-1)))

Otherwise,

if(excelMatrix[r][c] == sumList.get(c-1))

You didn't specify types in the example code so I assumed it like:

    String[][] excelMatrix = {{"A", "0", "0", "6", "0"},
                              {"B", "0", "2", "0", "2"},
                              {"C", "0", "1", "0", "0"},
                              {"D", "3", "0", "0", "2"}};

    List<String> sumList = new ArrayList<>();
    sumList.add("3");
    sumList.add("3");
    sumList.add("6");
    sumList.add("4");

And modified your for loop like this:

    List<String> checkList = new ArrayList<>();

    for(int c = 0; c < excelMatrix.length; c++){
        for(int r = 1; r < excelMatrix[0].length; r++){
            if(excelMatrix[c][r].equals(sumList.get(r-1))){
                 checkList.add(excelMatrix[c][0]);
            }
        }
    }

At the end

    for(int i = 0; i<checkList.size(); i++){
        System.out.print(checkList.get(i));
    }

gives

AD

This is completely unrelated to the answer you are looking for. The code you are using to calculate the sum of the column elements is wrong. Use the below code to get the proper list of sums.

   List sumList = new ArrayList();
        for(int c = 0; c < excelMatrix.length; c++){
            int sum = 0;
            for(int r = 0; r < excelMatrix.length; r++){
                sum = excelMatrix[r][c] + sum;
                System.out.println(sum);

            }
            sumList.add(sum);
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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