如何计算 java 中前 7 个值之后的 7 个最近值的移动平均值?
How do I calculate the moving average of the 7 most recent values after the first Seven values in java?
我的问题是如何使用 ArrayList 计算前 7 个值之后的 7 个最近值的移动平均值?
这是我的前七个值的代码,我无法理解的是我如何获得前七个值之后的 7 个最近值的平均值,如果有人能帮助我,我将不胜感激这个。
import java.util.ArrayList;
导入java.io.*;
导入java.util.Scanner;
public class 移动平均线{
public static void main(String[]args) throws FileNotFoundException{
int sum = 0;
Scanner s = new Scanner(new File("values.txt"));
ArrayList<Integer> values = new ArrayList<>();
ArrayList<Integer> averages = new ArrayList<>();
System.out.println("\tValue"+"\t\tRolling Average");
System.out.println();
while(s.hasNext())
{
values.add(s.nextInt());
}
for(int i = 0; i<7;i++)
{
sum = sum + values.get(i);
averages.add(sum/(i+1));
System.out.println("\t"+values.get(i)+"\t\t"+averages.get(i));
}
}
}
假设您有 N
(>=K) 个数字,并且想要计算滚动平均值,那么下面是一个简单的过程:
import java.util.*;
class Main
{
public static void rollingAverage(ArrayList<Integer> list, int K)
{
int sum = 0;
int avg = 0;
int N = list.size();
// This part calculates the average of first (i+1) values
for (int i=0; i<K; i++)
{
sum += list.get(i);
avg = sum / (i+1);
}
// This part calculates the rolling average (average of `K` recent values)
for (int i=K; i<N; i++)
{
sum -= list.get(i-K); // Subtracting `i-k`th value
sum += list.get(i); // Adding current number
avg = sum / K;
System.out.println( avg );
}
}
public static void main (String [] arg)
{
ArrayList<Integer> list = new ArrayList<>
(Arrays.asList(10,10,10,10,10,10,10,80,150,220));
rollingAverage( list , 7 ); // O/P : 20 40 70
}
}
您可以使用上述方法轻松实现滚动平均程序。
有任何疑问请随时提出。
How do I calculate the average of the 7 most recent values after the first Seven values in java?
这保证您有足够的条目。然后它简单地对适当的子列表进行平均,按计算打印出来。
List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14);
if (list.size() < 14) {
System.out.println("Insufficient values provided");
} else {
int count = 1;
double sum = 0;
for (int val : list.subList(7, 14)) {
sum += val;
System.out.printf("%d running average = %.2f%n", val,
sum / count++);
}
}
对于这个特定的数据,打印
8 running average = 8.00
9 running average = 8.50
10 running average = 9.00
11 running average = 9.50
12 running average = 10.00
13 running average = 10.50
14 running average = 11.00
我的问题是如何使用 ArrayList 计算前 7 个值之后的 7 个最近值的移动平均值?
这是我的前七个值的代码,我无法理解的是我如何获得前七个值之后的 7 个最近值的平均值,如果有人能帮助我,我将不胜感激这个。
import java.util.ArrayList;
导入java.io.*;
导入java.util.Scanner;
public class 移动平均线{
public static void main(String[]args) throws FileNotFoundException{
int sum = 0;
Scanner s = new Scanner(new File("values.txt"));
ArrayList<Integer> values = new ArrayList<>();
ArrayList<Integer> averages = new ArrayList<>();
System.out.println("\tValue"+"\t\tRolling Average");
System.out.println();
while(s.hasNext())
{
values.add(s.nextInt());
}
for(int i = 0; i<7;i++)
{
sum = sum + values.get(i);
averages.add(sum/(i+1));
System.out.println("\t"+values.get(i)+"\t\t"+averages.get(i));
}
}
}
假设您有 N
(>=K) 个数字,并且想要计算滚动平均值,那么下面是一个简单的过程:
import java.util.*;
class Main
{
public static void rollingAverage(ArrayList<Integer> list, int K)
{
int sum = 0;
int avg = 0;
int N = list.size();
// This part calculates the average of first (i+1) values
for (int i=0; i<K; i++)
{
sum += list.get(i);
avg = sum / (i+1);
}
// This part calculates the rolling average (average of `K` recent values)
for (int i=K; i<N; i++)
{
sum -= list.get(i-K); // Subtracting `i-k`th value
sum += list.get(i); // Adding current number
avg = sum / K;
System.out.println( avg );
}
}
public static void main (String [] arg)
{
ArrayList<Integer> list = new ArrayList<>
(Arrays.asList(10,10,10,10,10,10,10,80,150,220));
rollingAverage( list , 7 ); // O/P : 20 40 70
}
}
您可以使用上述方法轻松实现滚动平均程序。 有任何疑问请随时提出。
How do I calculate the average of the 7 most recent values after the first Seven values in java?
这保证您有足够的条目。然后它简单地对适当的子列表进行平均,按计算打印出来。
List<Integer> list = List.of(1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14);
if (list.size() < 14) {
System.out.println("Insufficient values provided");
} else {
int count = 1;
double sum = 0;
for (int val : list.subList(7, 14)) {
sum += val;
System.out.printf("%d running average = %.2f%n", val,
sum / count++);
}
}
对于这个特定的数据,打印
8 running average = 8.00
9 running average = 8.50
10 running average = 9.00
11 running average = 9.50
12 running average = 10.00
13 running average = 10.50
14 running average = 11.00