如何遍历多维数组的列[行] [英] How to iterate through columns [rows] of a multi dimensional array

查看:98
本文介绍了如何遍历多维数组的列[行]的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用多维数组存储特定销售人员(1至4个销售人员)销售的产品总数(产品范围1至5).

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在第1到4行中排列了salesPerson,在第1到5列中排列了产品ID.

T arranged the salesPersons in rows 1 to 4, and Product IDs in columns 1 to 5.

我唯一不能做的就是专门遍历行以获取每个产品的总数,即列1:行1至4的总和=总产品1,列2:行1至4的总和= product2总等等.

Only thing I can not do is iterate exclusively through the rows to get the total of each product ie column 1: sum of rows 1 to 4= Total product 1, column 2: sum of rows 1 to 4= product2 Total etc.

请参见测试salesTestTest应用程序代码,后跟Sales类:

See test salesTest application code followed by the class Sales:

/*
test application for sales class
*/
package salestest;

import SalesLibary.Sales;


public class SalesTest {


    public static void main(String[] args) {
        // pass monthly stats to 4r(salespesons) * c5(products 1 to 5) using initialization method
        int monthlySales [][]=  {{13, 23, 45, 67, 56},
                                {43, 65, 76, 89, 90},
                                {43, 45, 76, 98, 90},
                                {34, 56, 76, 43, 87}};
        //pass default values to constructor when creating object of class
        Sales companySales = new Sales("Monneys Inc.", monthlySales);
        companySales.displayMessage();
        companySales.displaySales();
        }//end main
    }//end SalesTest class

    //class Sales with associated methods 
/*
 Chapter 7: Practical Question 2
 */
package SalesLibary;


public class Sales {

//declare fields/members
private int salesTotals[][];
private String companyName;

//passs string and two dimensional array of sales stats to constructor from application object
public Sales(String name, int monthlySales[][]) {
    companyName = name;
    salesTotals = monthlySales;


}//end constructor

public void setCompanyName(String name) {
    companyName = name;

}

public String getCompanyName() {
    return companyName;
}

public void displaySales() {
    //table heading
    System.out.printf("The monthly sales stats for company %s are: ", companyName);
    System.out.println("                                                         ");//set columns headings
    //create column headings representing products sold 1 to 5 by looping thru each colmn of row(salsperson)
    System.out.print("            ");
    for (int product = 0; product < salesTotals[0].length; product++) {
        System.out.printf("Product %d  ", product + 1);
    }
    System.out.println("Total ");

    //create rows of table represnting salespersons 1 too 4, ten loop through array and print element
    for (int salesPerson = 0; salesPerson < salesTotals.length; salesPerson++) {
        System.out.printf("SalesPerson %2d", salesPerson + 1);


        //use nested for loop to output all results
        for (int total : salesTotals[salesPerson]) {
            System.out.printf("%10d", total);
        }
        //call method to get total for each sales person by passing
        //a row of products sold for each sales person to method
        double total = getTotal(salesTotals[salesPerson]);
        System.out.printf("%10.2f\n", total);


    }//end outer for
    System.out.println("Product Total: ");
    double productSum = getTotalProduct();
    System.out.printf("%10.2f", productSum);
    //enumerate through each column and get sum to represent product total


}//end method Display sales

//method to calculate total, argument is array of results
public double getTotal(int salesTotals[]) {
    int total = 0;
    //loop thru array passed
    for (int count : salesTotals) {
        total += count;
    }

    return total;
}// end get salesPerson tital

//display message
public void displayMessage() {
    System.out.printf("\nWlecome to %s monthly sales summaries!!!\n\n", getCompanyName());
}//end display message

//get total product sold
public double getTotalProduct() {
    int productTotal[];
    int totalProduct = 0;
    //loop through array passed
    for (int salesPerson = 0; salesPerson < salesTotals.length; salesPerson++) {
        //go through each column of row[row]
        productTotal = salesTotals[salesPerson];
        //loop thirugh product total and get sum
        for (int count : productTotal) {
            totalProduct += count;
        }

    }//end outer for loop   
    return totalProduct;
}// end get salesPerson total
}//end Sales class

推荐答案

要遍历二维数组中的单行k:

To iterate over a single row k in a two-dimensional array:

for (int j = 0; j < multiarray[k].length; j++)
    multiarray[k][j]; // do something

并遍历二维数组中的单列k:

And to iterate over a single column k in a two-dimensional array:

for (int i = 0; i < multiarray.length; i++)
    multiarray[i][k]; // do something

这篇关于如何遍历多维数组的列[行]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆