Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/349.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何遍历多维数组的列[行]_Java - Fatal编程技术网

Java 如何遍历多维数组的列[行]

Java 如何遍历多维数组的列[行],java,Java,我使用多维数组存储特定销售人员(1到4名销售人员)销售的产品总量(产品范围1到5) T将销售人员排在第1至第4行,产品ID排在第1至第5列 我唯一做不到的就是以独占方式遍历行以获得每个产品的总数,即第1列:第1到4行之和=总产品1,第2列:第1到4行之和=总产品2等 请参阅test salesTest应用程序代码,然后是Sales类: /* test application for sales class */ package salestest; import SalesLibary.Sal

我使用多维数组存储特定销售人员(1到4名销售人员)销售的产品总量(产品范围1到5)

T将销售人员排在第1至第4行,产品ID排在第1至第5列

我唯一做不到的就是以独占方式遍历行以获得每个产品的总数,即第1列:第1到4行之和=总产品1,第2列:第1到4行之和=总产品2等

请参阅test salesTest应用程序代码,然后是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
/*
销售类考试申请表
*/
包装销售测试;
进口销售文库。销售;
公共类销售测试{
公共静态void main(字符串[]args){
//使用初始化方法将每月统计数据传递给4r(销售人员)*c5(产品1到5)
int monthlySales[][]={{13,23,45,67,56},
{43, 65, 76, 89, 90},
{43, 45, 76, 98, 90},
{34, 56, 76, 43, 87}};
//创建类的对象时将默认值传递给构造函数
销售公司销售额=新销售额(“Monneys Inc.,月销售额”);
companySales.displayMessage();
companySales.displaySales();
}//端干管
}//结束测试类
//使用关联的方法初始化Sales
/*
第7章:实际问题2
*/
包装销售文库;
公务舱销售{
//声明字段/成员
私人整数销售总额[];
私有字符串companyName;
//将字符串和二维销售统计数据数组从应用程序对象传递给构造函数
公开销售(字符串名称,int monthlySales[]]){
companyName=名称;
销售总额=月销售额;
}//端构造函数
public void setCompanyName(字符串名称){
companyName=名称;
}
公共字符串getCompanyName(){
返回公司名称;
}
公开销售(){
//表格标题
System.out.printf(“公司%s的月度销售统计数据为:”,companyName);
System.out.println(“”;//设置列标题
//通过循环遍历行(salsperson)的每一列,创建表示1到5个销售产品的列标题
系统输出打印(“”);
对于(int product=0;product
要在二维数组中迭代一行
k

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

首先,如果不需要,不要使用所有代码。您只需要声明,可能还需要一个for循环

循环通过列:

for(int i=0; i<monthlySales[salesPerson].length; i++) {
    monthlySales[i][salesPerson]; //do something with it!
}
for(int i=0;i得到它:)
有点头晕目眩试图想象阵列,但最终到达了那里,为帮助欢呼

//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
//循环遍历每列以获得销售的产品总数
对于(int product=0;product
假设您有一个二维数组作为

int[][] salesData={{13, 23, 45, 67, 56},
                                {43, 65, 76, 89, 90},
                                {43, 45, 76, 98, 90},
                                {34, 56, 76, 43, 87}};
这是一个4*5的矩阵

int[0]表示第一行的值,即{13,23,45,67,56},如果需要单个单元格中的值,则需要为每个循环重复2次,如

    for (int[] rowData: salesData){
                for(int cellData: rowData)
                {
System.out.printn("the indiviual data is" +cellData);
                }
            }

@shutefan重要的是最后一次编辑,我及时发现了错误。我知道这与结果无关,但为什么不把答案想清楚然后发送。。。通过这种方式,我多次重新键入注释以指出错误。任何人都可以通过重复//do something代码,以简洁的方式在一个循环中执行行和列迭代,这给了他们额外的分数。相关:按基数方向自由遍历2D数组(下、上、左、右):