将for循环中的变量分配给arraylist中每个点的x坐标

Assigning variable in for loop to the x coordinate of each point in an arraylist

我正在尝试将 ArrayList 中的每个 x 和 y 坐标分配给一个变量,因为每个坐标都将与用户在屏幕上单击的点的坐标进行比较。

    // the coordinates of each point
    Point p1 = new Point(100, 100);
    Point p2 = new Point(100, 400);
    Point p3 = new Point(400, 100);
    Point p4 = new Point(400, 400);

    // the points are added to the arraylist
    mPoints.add(p1);
    mPoints.add(p2);
    mPoints.add(p3);
    mPoints.add(p4);
}
//this is the content of my arraylist

@Override
public boolean onTouchEvent(MotionEvent event)
    {

      //motionevent detects motion from the user
      //float x;
      // x = event.getX();
      //float y;
      //y = event.getY();

      switch (event.getAction())
      {
        case MotionEvent.ACTION_UP:
            //drawLine(x, y);
            //mContext = this.mContext;
            float Cox = event.getX();
            float Coy = event.getY();

            Double nearestDistance = 1000.12; //this is hardcoded for the sake of 
                                              //  declaring the variable.
            int NearestPoint =  -1;
            // int NearestPoint =  mPoints.get() ;
            for (int i = 0; i<mPoints.size(); i++)
            {
                float xi;
                float yi;
                xi = mPoints.x;
                yi = mPoints.y;
            }

//the assignments above are my problem

如果我没理解错的话,你的 for 循环需要看起来像

for (int i = 0; i<mPoints.size(); i++)
{
    float xi = mPoints.get(i).x;
    float yi = mPoints.get(i).y;
    System.out.println("Point " + i + " y: " + yi);
    System.out.println("Point " + i + " x: " + xi);
}

这个 for 循环将获取 ArrayList 中的每个 Point 并将其打印到控制台。除了打印之外,您还需要根据需要调用您的方法。