0

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

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;
}
4

1 回答 1

2

移入 PointF xy = new PointF();for 循环以在 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 
于 2013-03-07T12:06:43.417 回答