There are two existing JSONArrays with the same format
One is newly added data
saveArray: [{"name":"name1","value":10},{"name":"name2","value":12},...,{}]
One is the average data
avgArray:[{"name":"name1","value":11},{"name":"name2","value":13},...,{}]
Know the number to calculate the average int num = 10;
Then based on the new data, after calculating the average value, update the existing average record:
Finally got:
newAvgArray: [{"name":"name 1","value":(10 x 11 10)/11},{"name":"name 2","value":(13 x 10 12) /11},...,{}]
The only way I can think of is:
for (int i = 0;i < avgArray.size();i++){
avgObj = avgArray.get(i).get("value");
addValue = saveArray.get(i).get("value");
//然后计算新的值保存新的Array
}
Is there a better way to calculate
No more.
Algorithmically speaking, this is the most simplified. O(n)
There should be a master who can use lambda to perform the arrogant operation, but as far as your requirements are concerned, you can only recalculate the value. In addition, you need to review the questions more.
The following operation is not allowed. Make sure that the value of name can match.