How to iterate through columns [rows] of a multi dimensional array

后端 未结 4 1550
庸人自扰
庸人自扰 2020-12-05 21:07

I am using a multi-dimensional array to store the total amount of product sold (products range 1 to 5) by a particular salesperson (1 to 4 salespersons).

T arranged

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-05 22:07

    Got it:) Bit of a headwreck trying to visualise the array but got there eventually, cheers for the help

    //loop thorugh each column to get total products sold
        for (int product = 0; product < salesTotals[0].length; product++) {
            int totalProduct = 0;
            for (int salePerson = 0; salePerson < salesTotals.length; salePerson++) {
    
                totalProduct += salesTotals[salePerson][product];
    
            }//end inner for
            System.out.printf("%10d", totalProduct);
    
        }//end outer for
    

提交回复
热议问题