简体   繁体   English

二维阵列工艺?

[英]2-D Array Processes?

I'm sure this is a pretty dumb question, but one of the questions on my homework is throwing me off (not looking for the answer, just some clarification on what the question might be asking). 我敢肯定这是一个很愚蠢的问题,但是我作业中的一个问题使我无法工作(不是在寻找答案,只是在澄清可能要问的问题)。 My professor tends to word things in a way that I've never heard things phrased before, so this question is kind of confusing me. 我的教授倾向于以一种我从未听过的措词来表达事物,所以这个问题有点使我感到困惑。

The questions goes like this: 问题是这样的:

Consider the following declarations:
final int carTypes = 5;
final int colorTypes = 6;
double [][] sales = new double[carTypes][colorTypes];

To sum the sales by carTypes, what kind of processing is required?
To sum the sales by colorTypes, what kind of processing is required?

Again, I'm not asking for the answer, I've just never come across a question worded this way so was hoping someone could point me in the right direction as to what this is asking. 再说一次,我不是要答案,我只是从来没有遇到过用这种方式措辞的问题,所以希望有人可以向我指出正确的方向。 Thanks in advance. 提前致谢。

Think of it like accessing a spreadsheet. 将其视为访问电子表格。 The rows are car types, and the columns are color types. 行是汽车类型,列是颜色类型。

To get all of the values of either one, you're iterating over some portion of the 2D array - either in a row-major or column-major fashion. 要获取任何一个的所有值,您要遍历2D数组的某些部分-以行为主或列为主的方式。

For example, if I wanted to add up all of the sales of a particular car type, I would iterate across the structure given an anchor value for the car type I want. 例如,如果我想将某个特定汽车类型的所有销售额加起来,则在给定所需汽车类型的锚定值的情况下,对结构进行迭代。

The loop would look something like this: 循环看起来像这样:

double sum = 0;
int carTypeDesired = 2;
for(int i = 0; i < sales[carTypeDesired].length; i++) {
    sum += sales[carTypeDesired][i];
}

It's a similar operation to do the summation by a specific color type, as well. 同样,也可以通过特定的颜色类型进行求和。

If you wanted a grand total of all cars, independent of make or color, then you would loop over all elements in the array and add them together. 如果您想获得所有汽车的总数,而与品牌或颜色无关,那么您将遍历数组中的所有元素并将它们加在一起。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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