2015-08-23 161 views
1

enter image description here 是否有可能遍历一个ArrayList添加不是所有的实例,但每12?有很多线程使用addAll来添加所有的实例,但不是部分。迭代ArrayList增加值

我目前有包含数百个浮点值的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) 
    { 
    ?????????????????? 
    } 
    } 

任何帮助,将不胜感激我似乎无法在任何地方找到这样的东西,因为我觉得所有实例的总和就是这样一个热门的话题。它可能是一个正确迭代的例子。关于总结,我会在C++中使用accumulate,但不知道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>

回答

3

要做到这一点,最好的办法是使用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); 
} 
+0

当你用'1'开始计数器时'counter <= MAX'应该是条件。 – YoungHobbit

+0

已修复。快速打字 –

+0

非常感谢您的支持!然而,我有关于一个问题: '迭代器吧= rValue.iterator();' 类型不匹配:无法从iterator 转换为迭代器 Abbie

3

我会建议你使用doubleDouble而不是浮动,因为它有一半左右一万亿次的准确性。

可以总结12的每块这样

public static List<Double> sumBlocks(List<Double> list, int blockSize) { 
    List<Double> ret = new ArrayList<>(); 
    for(int i = 0; i < list.size(); i += blockSize) { 
     double sum = 0; 
     for(int j = 0, len = Math.min(list.size() - i, blockSize); j < len; j++) 
      sum += list.get(i + j); 
     ret.add(sum); 
    } 
    return ret; 
} 

,并呼吁

List<Double> sums = sumBlocks(list, 12); 
+3

'数学。 min'有一个参数? – ajb

+0

@ajb感谢您指出这一点 –

+0

您不能使用'double'或任何具有泛型的基本类型,它必须是对象类型 – YoungHobbit

0

,ArrayList对象继承子表(开始,结束)从List类方法。您可以使用myList.sublist(i,j)访问子列表并获得您的总和。从那里,它应该只是简单的算术来获得你的迭代。在for循环中,它应该是:myList.sublist(i * 12,i * 12 + 12)。

//Input list 
ArrayList<Float> inputList = new ArrayList<Float>(); 

ArrayList<Float> result = new ArrayList<Float>(); 
    int groupSize = 12; 
    int offset=0; 
    while(offset < inputList.size()) { 
     int toIndex = (inputList.size()-offset)>=groupSize? offset+groupSize : inputList.size(); 
     result.add(listSum(inputList.subList(offset, toIndex))); 
     offset += groupSize; 
    } 

助手方法添加列表中的项目

static float listSum(List<Float> ar) { 
    float accumulator = 0f; 
    for(float item:ar) { 
     accumulator += item; 
    } 
    return accumulator; 
} 
2

只是为了展示另一种方式来实现:

public static List<Double> sumBlocks(List<Double> list, int blockSize) { 
    List<Double> result = new ArrayList<>(); 
    double sum = 0d; 
    for (int i = 0; i < list.size(); i++) { 
     if (i > 0 && i % blockSize == 0) { 
      result.add(sum); 
      sum = 0d; 
     } 
     sum += list.get(i); 
    } 
    result.add(sum); 
    return result; 
} 
0
Lista<Double> list = // original list 
List<Double> ret = new ArrayList<>(); 

int counter = 0; 
double sum = 0; 

for (Double f : list) { 
    if (counter == 12) { 
     sum = 0; 
     counter = 0; 
     ret.add(sum); 
    } 
    sum += f; 
    counter++; 
} 
// if list is not a multiple of 12 
if (list.size() % 12 != 0) { 
    ret.add(sum); 
} 
return ret; 
0

试试这个:

public float total; 

for(int i; i < rValue.Size(); i ++) 
{ 
    total += rValue[i]; 

if(i%12=0) 
    { 
    add total to new ArrayList 
    total = 0; 
    } 
}