Hi everyone!
Another question regarding 2D arrays: this time I need to calculate the number of particular elements in each row and append them to an empty array.
For example, if initial array is
int [][] owners = {{1,1,1,2},
{1,1,2,2}};
it is required to calculate how many times "1" appears in each row => the resulting array should give {3,2}.
The code is here, but it returns cumulative result {3,5} instead:
int [] shares = new int [owners.length];
int counter=1;
for (int i=0; i<owners.length;i++){
for (int j=0; j<owners[i].length;j++){
if (owners[i][j] == 1) {
shares[i]=counter;
counter++;
}
}
}
What I am doing wrong here? Thanks a lot in advance!
1