gpt4 book ai didi

java - 迭代 ArrayList 添加值

转载 作者:行者123 更新时间:2023-11-29 10:11:35 25 4
gpt4 key购买 nike

enter image description here是否可以遍历 ArrayList 不是添加所有实例而是每 12 个实例?有很多关于使用 addAll 添加所有实例但不添加部分的话题。

我目前有一个包含数百个浮点值的 ArrayList:

片段:

120.5, 22.2, 76.2, 64.5, 38.3, 27.1, 149.4, 62.3, 127.9, 79.1, 83.4, 68.3, 61.0, 83.4, 5.4, 83.8, 78.3, 111.8, 104.1, 145.2, 94.3, 20.0, 104.7, 35.9, 68.6, 10.1, 41.1, 82.2, 170.7, 17.2, 122.1, 61.0, 46.3, 101.1, 59.0, 30.0, ...

我想做的是对前 12 个实例求和并将其放入一个新的 ArrayList 中,对接下来的 12 个实例求和,将其存储到新创建的 ArrayList 中,依此类推。正好有 996 个实例,所以我应该在这个新 ArrayList 中有 83 个新值 (996/12=83)。

这能做到吗?如果是这样怎么办?这是我必须...

  // ArrayList that contains the float values shown above

public MonthData depthValues() {
ArrayList<Float> rValue = new ArrayList<>();
for (int i = 0; i<months.size(); i++)
{
rValue.add(months.get(i).getDepthMM());
}
System.out.println(rValue);
System.out.println(rValue.size());
return null;

}

//New arrayList im trying to make
//probably done this wrong, help needed here

public MonthData depthTotals() {
ArrayList<Float> depthAdd = new ArrayList<Float>();

int t = 12;

for(int i = 0; i<rValue.size(); ++i)
{
??????????????????
}
}

任何帮助将不胜感激 我似乎无法在任何地方找到任何关于此的内容,因为我认为所有实例的总和是一个如此受欢迎的话题。这可能是正确迭代的情况。关于求和,我会使用 accumulate在 c++ 中,但不知道在 java 中的等效项(如果有的话)。感谢您提前提供任何建议/帮助!

更多代码:

public class WeatherStation {
private ArrayList<MonthData> months;
private ArrayList<MonthData> rValue;
private ArrayList<MonthData> depthAdd;

MonthData 是读取到此类的数据模型,它由许多 getter 组成....

public class MonthData {

int y;
int m;
float h;
...


public MonthData(String data) throws Exception {
...
this.parseData(data);
}


void parseData(String csvData) {
String[] parseResult = csvData.trim().split("\\s+");

this.setYear(parseResult[0]);
this.setMonth(parseResult[1]);
...


public String toString() {
return "y =" + year + ", m =" + month + ",...

}


public int getY() {
return y;
}

// followed by lots of getters for: m, h, c, f, r, s, ...




public MonthData depthValues() {
ArrayList<Float> rValue = new ArrayList<>();
for (int i = 0; i<months.size(); i++)
{
rValue.add(months.get(i).getDepthMM());
}
System.out.println(rValue);
System.out.println(rValue.size());
return null;

代码推荐:

 public MonthData depthTotals() {
ArrayList<Float> depthAdd = new ArrayList<>();
Iterator<Float> it = rValue.iterator();
final int MAX = 12;
while (it.hasNext()){
float sum = 0f;
int counter = 1;
//iterating 12 times
//still check if there is an element in list
while (counter < MAX && it.hasNext()){
sum += it.next();
counter++;
}
depthAdd.add(sum);}
}

问题:Iterator<Float> it = rValue.iterator();

类型不匹配:无法从 Iterator<MonthData> 转换至 Iterator<Float>

最佳答案

执行此操作的最佳方法是使用 Iterator12 的计数器(通过使用 while)。这是一个例子:

List<Float> yourList = ...;
// fill yourList
List<Float> results = new ArrayList<>();
Iterator<Float> it = yourList.iterator();
final int MAX = 12;
while (it.hasNext()) {
float sum = 0f;
int counter = 1;
//iterating 12 times
//still, check if there's an element in your list
while (counter <= MAX && it.hasNext()) {
sum += it.next();
counter++;
}
result.add(sum);
}

关于java - 迭代 ArrayList 添加值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32162861/

25 4 0