Pass cycle result For a one-dimensional ARRAY

1

I have a Bidimensional array called "info", "n" rows and "7" columns. Which I go through with the following code:

Note: I start to go through the array starting from column "1";

for (int x=0; x < info.length; x++){  
double a=0.0, b=0.0;  
for (int y=1; y < info[x].length; y++){  
double numEntero = Double.parseDouble(info[x][y]);  
a=(a+numEntero);  
b=(a/7);  
}  
System.out.println(b);  
}  

At the end of the for cycle, he throws this at me:

9.857142857142858  
9.257142857142856  
7.914285714285713  
8.285714285714286  
8.085714285714285  
9.285714285714283  
9.057142857142859  
9.4  
10.0  

As you may notice, the original arrangement is String, (which contains numbers) and I convert to double the data stored in it. I add the columns from "1" to "7" (since I omit column 0) and divide them by "7". At the end I store them in a double variable called "b".

Is there any way to pass the variable "b" to a new one-dimensional array ?, that is, to pass this result to a new Array. Will it be possible?

9.857142857142858  
9.257142857142856  
7.914285714285713  
8.285714285714286  
8.085714285714285  
9.285714285714283  
9.057142857142859  
9.4  
10.0  

I would greatly appreciate your help:)

    
asked by Alberto 24.11.2018 в 18:32
source

2 answers

1

Declare the array before the cycle and fill it in every loop path.

double[] miArray = new double[info.length]; int c=0;
for (int x=0; x < info.length; x++){  
  double a=0.0, b=0.0;  
  for (int y=1; y < info[x].length; y++){  
   double numEntero = Double.parseDouble(info[x][y]);  
   a=(a+numEntero);  
   b=(a/7);  
   miArray[c] = b; c = c+1;
  }
}  
    
answered by 24.11.2018 / 19:02
source
0

Thank you very much everyone for your guidance. It has really been very useful for me. I told you that the solution I found thanks to your help was the following:

double [] miArray = new double [info.length];
int c = 0;
for (int x = 0; x < info.length; x ++) { double a = 0.0, b = 0.0;
for (int y = 1; and < info [x] .length; and ++) {
double numEntero = Double.parseDouble (info [x] [y]);
a = (a + numEntero);
b = (a / 7);
miArray [c] = b;
}
c ++;
}

Thanks Carmen, regards! :)

    
answered by 24.11.2018 в 21:11