2013-03-18 15 views
5

多次元配列を使用して、特定の営業担当者(1〜4名の営業担当者)が販売した製品の総量(製品範囲1〜5)を格納しています。多次元配列の列[行]を反復する方法

Tは、私は、各製品すなわちコラム1の合計を取得するために行を独占的に反復されることができません5.

だけのものに列1の行1〜4で販売員、およびプロダクトIDを配置し:行1〜4の和=全生成物1、カラム2:行1〜4の和=総product2等

クラス販売続い

参照試験salesTestアプリケーションコード:

/* 
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 
+0

関連様ループごとに2を反復する必要があります:基底方向(下、上、左、右)で2D配列を自由に移動する:http://stackoverflow.com/questions/22253140/how-to-freely-traverse-the-elements-in-a-two-dimensional -array-by-cardinal-direc – aliteralmind

答えて

20

オーバー反復します単一の列kが2次元配列:

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

そして、2次元配列内の単一の列kを反復処理するために:それが必要ない場合は

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

@shutefan最後に編集したものがあり、そのエラーを時間通りに見つけました。 –

+0

結果については問題ではありませんが、答えを考えて送信してみませんか?このようにして、エラーを指摘するために何度かコメントを再入力しました。 – shutefan

+0

まだ、明確かつ簡潔な説明のために+1。 – shutefan

1

まず第一には、すべてのコードを使用しないでください。あなたは宣言が必要で、たぶんfor-loopを必要とします。カラムを通す

ループ:

for(int i=0; i<monthlySales[salesPerson].length; i++) { 
    monthlySales[i][salesPerson]; //do something with it! 
} 
0

は:)配列を視覚化しようとしているheadwreckの ビットを、それを手に入れたが、最終的にそこに着いた、助け

//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 
6

ための歓声は、あなたが持っているとしましょう2次元配列として

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}、すなわち1行目を表す個々のセルの値を必要とする場合は、

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