简体   繁体   中英

How do you iterate through a 2D array from a specific point?

样本数据表

The following image was created rather hastily, but the premise of my question is outlined within. What I am looking to do is iterate through to a specific part in my CSV file I have loaded into Eclipse and once there, collect the data in that column and average the numerical values and print to the console. At first I had thought about manually entering ([1][1] + [1][2] + [1][3]) / 3 , but this for obvious reasons would be too time consuming. Could I create a loop which moves over 1 column and down 1 row and then start computing from there?

As the data is given row by row in CSV, it would be best to process it in that way.

Psuedocode:

int columnCount = loadHeaders().length;
float[] totals = new float[columnCount];
int rowCount = 0;
for each row {
  rowCount++;
  for each column //skipping first
     totals[rowIndex] += row[columnIndex].data; //add cell to totals
}
float[] averages = totals / rowCount;

我相信您必须将循环变量的初始值指定为2,除非我误解了这个问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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