在List中保存数据时出错

时间:2013-03-07 12:01:17

标签: android

我有一个函数,我用来计算点的线性函数。我想返回所有值的列表。但我有一个错误,在我的列表中只保存了一个点。你能告诉我出了什么问题吗?为什么在最后一张表中只保存一分? 这是代码:

private List<PointF> forumlaOfFunctionCharts(Number[] points){
    List<PointF> listOfPoints = new ArrayList<PointF>();
    PointF xy = new PointF();
    float a = 0, b = 0, y = 0, y1 = 0, y2 = 0;
    for(int i = 0; i<points.length-1; i++){
        y1 = points[i].intValue();
        y2 = points[i+1].intValue();

        a = (y1-y2)/(i-(i+1));
        b = y1 - (a*i);

        Log.v("daneTabela", ""+a+", "+b);
        for(float j = i; j<=i+1; j=j+0.1f){
            y = a * j + b;
            xy.set(j, y);
            listOfPoints.add(xy);
            Log.v("daneTa", ""+xy.x+", "+xy.y);
        }
    }

    return listOfPoints;
}

1 个答案:

答案 0 :(得分:2)

在for循环中移动PointF xy = new PointF();以在ArrayList中添加新对象:

 PointF xy ; 
 //..your code here...
 Log.v("daneTabela", ""+a+", "+b);
        for(float j = i; j<=i+1; j=j+0.1f){
            y = a * j + b;
             xy = new PointF();  //<<< create PointF object here
            xy.set(j, y);

            listOfPoints.add(xy);
            Log.v("daneTa", ""+xy.x+", "+xy.y);
        }
 //...your code here