用ArrayList的内容填充一个二维数组。

huangapple go评论73阅读模式
英文:

Filling a 2d array with contents from an ArrayList

问题

以下是您提供的内容的翻译:

我有一个按以下格式排列的文件:

姓名|测试1|测试2|测试3|测试4|测试5|测试6|测试7|测试8|测试9|测试10	
约翰·史密斯|82|89|90|78|89|96|75|88|90|96
简·多e|90|92|93|90|89|84|97|91|87|91
约瑟夫·克鲁兹|68|74|78|81|79|86|80|81|82|87
苏珊娜·阮|79|83|85|89|81|79|86|92|87|88
理查德·佩雷斯|100|84|73|81|92|84|95|96|95|100
伊万·戴尔|77|91|90|75|97|94|76|89|90|92
克雷格·帕尔默|91|84|98|89|82|75|78|96|100|97
玛德琳·罗杰斯|75|79|78|93|91|76|80|88|100|81
切尔西·洛萨斯|87|94|89|96|95|85|88|92|86|86
贾斯珀·鲍蒂斯塔|100|83|93|100|98|97|96|97|97|98

我创建了一个ArrayList,从上表中从左到右填充了整数。我确认所有数字都在ArrayList中。我想要实现的是获取每列的平均值,因此我认为使用二维数组是可行的,但我在弄清楚正确的for循环语法以正确填充数组方面遇到了困难。

如果有人除了我的解决方案之外有任何其他解决方案,或者愿意帮助我,我会非常感激!

编辑:我尝试了以下内容:

int len = tests.length;
for(int i = 0; i < len; i++){
      for(int j = 0; j < len; j++)
      {tests[i][0] = (Double) testScores.get(i);
       tests[0][j] = (Double) testScores.get(j);
      }
    }

它产生了以下输出:

[[82.0, 89.0, 90.0, 78.0, 89.0, 96.0, 75.0, 88.0, 90.0, 96.0], [89.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [90.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [78.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [89.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [96.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [75.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [88.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [90.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [96.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]
英文:

I have a file that is formatted as follows:

Name|Test1|Test2|Test3|Test4|Test5|Test6|Test7|Test8|Test9|Test10	
John Smith|82|89|90|78|89|96|75|88|90|96
Jane Doe|90|92|93|90|89|84|97|91|87|91
Joseph Cruz|68|74|78|81|79|86|80|81|82|87
Suzanne Nguyen|79|83|85|89|81|79|86|92|87|88
Richard Perez|100|84|73|81|92|84|95|96|95|100
Ivan Dyer|77|91|90|75|97|94|76|89|90|92
Craig Palmer|91|84|98|89|82|75|78|96|100|97
Madeline Rogers|75|79|78|93|91|76|80|88|100|81
Chelsea Roxas|87|94|89|96|95|85|88|92|86|86
Jasper Bautista|100|83|93|100|98|97|96|97|97|98

I have created an ArrayList that is populated left to right with the integers from the table above. I confirmed that all the numbers are in the ArrayList. What I am trying to accomplish is getting the average of the columns, so I thought that using a 2d array is doable, but I am having a hard time figuring out the correct for-loop syntax to properly fill the array.

If anyone has any other solution apart from mine or would like to help me out, I would really appreciate it!

Edit: I have tried the following:

int len = tests.length;
for(int i = 0; i < len; i++){
      for(int j = 0; j < len; j++)
      {tests[i][0] = (Double) testScores.get(i);
       tests[0][j] = (Double) testScores.get(j);
      }
    }

Which yields this output:

[[82.0, 89.0, 90.0, 78.0, 89.0, 96.0, 75.0, 88.0, 90.0, 96.0], [89.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [90.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [78.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [89.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [96.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [75.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [88.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [90.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0], [96.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]]

答案1

得分: 2

for (int i = 0; i < 10; i++) {
    for (int j = 0; j < 10; j++) {
        tests[i][j] = (Double) testScores.get(i * 10 + j);
    }
}

this will work

if you want to calculate the mean of the i-th column:

double mean(int column_index) {
    double mean = 0;
    for (int i = 0; i < 10; i++) {
        mean = mean + tests[i][column_index];
    }
    return (mean / 10);
}
英文:
for(int i = 0; i &lt;10  ; i++){
    for(int j = 0; j &lt;10  ; j++){
      {tests[i][j] = (Double) testScores.get(i*10 +j);
      }
    }

this will work

if you want to calculate the mean of the i-th column :

double mean(column_index) {
    double mean = 0;
    for(int i = 0; i &lt;10  ; i++){
        mean = mean + tests[i][column_index];}
    return (mean/10) ;}

huangapple
  • 本文由 发表于 2020年8月27日 17:43:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/63613295.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定