在我的程序中,我试图从起始位置(0,0)查找最近的点,然后再次“移动”到下一个点。通过文件读取这些点。我要移至的下一个点是“最近”的点。我使用勾股定理找到距离。但是,我可以做些什么来“检查”要确定的点,以确定是否已经访问过该点。例如,如果点是0,0,然后到1,1,如何检查以“告诉”程序0,0不再是一个选项?

public class PointsNStuff {
    public static void main(String [] args) {

        final int P = StdIn.readInt();
        double [] x = new double[P];
        double [] y = new double[P];
        double [] visit= new double[P]; //Set an array that stores points that have been visited already
        double [] math= new double[P]; //Set an array that stores the distance to all the points


        for( int i= 0; i< P; i++){ //Store the values from the text file
            x[i] = StdIn.readDouble();
            y[i] = StdIn.readDouble();
        }

        double lowX = x[0];

        double lowY = y[0];

        double highX = x[0];

        double highY = y[0];

        //Find the lowest X and the lowest Y values:

        for (int i = 0; i < P; i++){
            if (lowX > x[i])
                lowX = x[i];
        }for (int i = 0; i < P; i++){
            if (lowY > y[i])
                lowY = y[i];
        }
        for (int i = 0; i < P; i++){
            if (highX < x[i])
                highX = x[i];
        }
        for (int i = 0; i < P; i++){
            if (highY < y[i])
                highY = y[i];
        }
        System.out.println(lowX + " " + lowY);
        System.out.println(highX + " " + highY);
        System.out.println("");
        System.out.println(P);

        //Determine the closest point
        double xCoord=0.0;
        double yCoord=0.0;
        double dist = -1.0;
        for (int i= 0; i < P; i ++){ //Repeat entire section for all P (number of points)
            for (int j = 0; j < P; j++){ //Find the distance between current point and all other points. Go through all points (do the math).
                xCoord = x[j]; // # x point
                yCoord = y[j]; // # y point
                double save= Math.sqrt( ( (xCoord+x[j]) * (xCoord+x[j]) ) + ( (yCoord + y[j]) * (yCoord + y[j]) ) ); //Pythagorean theorem
                save = math[j]; //store the distance in the array slot
            }
            for (int j = 0; j < P; j++){
                if (dist < math[j]){
                    dist = math[j];

                    //What boolean check can I put here to double check whether I have visited this point already?

                    xCoord = x[j]; // set the two points to what number they should be at.
                    yCoord = y[j];
                }
            }
            System.out.println(xCoord + " " + yCoord);
        }
    }
}


我还没有在名为“ visit”的数组中使用任何点。任何和所有帮助表示赞赏!谢谢!

最佳答案

使用ArrayList来存储点,

ArrayList<Double> x = new ArrayList<Double>();
ArrayList<Double> y = new ArrayList<Double>();

将点添加到arraylist,
for( int i= 0; i< P; i++){ //Store the values from the text file
  x.add(StdIn.readDouble());
  y.add(StdIn.readDouble());
}

从arraylist中选择点,
x.get(i); insted of x[i];
y.get(i); insted of y[i];

并删除已经使用的点,
x.remove(new Double(used_x_value));
y.remove(new Double(used_y_value));

Class ArrayList

10-06 14:00
查看更多