简体   繁体   中英

Filling a 2d array with contents from an ArrayList

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]]
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(column_index) {
    double mean = 0;
    for(int i = 0; i <10  ; i++){
        mean = mean + tests[i][column_index];}
    return (mean/10) ;}

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