0

我正在使用点类来管理 (x,y) 坐标列表,我需要按 X 的顺序对它们进行排序。

我在线阅读以创建一个实现 Comparator 的新类 PointCompare,但是我不确定它是如何工作的,因此我在 sortByXCoordinates 方法中有一个编译器错误。

非常感谢您的帮助,欢迎提出任何意见,在此先感谢。这是我的一些代码:

import javax.swing.JOptionPane;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
//import java.util.Iterator;

public class ConvexHullMain {

 private Point coordinates = new Point(0, 0);
 private final int MAX_POINTS = 3;
 private ArrayList<Point> coordinateList = new ArrayList<Point>();

 public void inputCoordinates() {

  String tempString; // temp string for JOptionPane
  int tempx = 0;
  int tempy = 0;

  for (int i = 0; i < MAX_POINTS; i++) {
   try {
    // input x coordinates
    tempString = JOptionPane.showInputDialog(null,
      "Enter X coordinate:");
    tempx = Integer.parseInt(tempString);

    // input y coordinates
    tempString = JOptionPane.showInputDialog(null,
      "Enter Y coordinate:");
    tempy = Integer.parseInt(tempString);

    coordinates.setLocation(tempx, tempy);// set input data into
              // coordinates object
    coordinateList.add(coordinates.getLocation()); // put in
                // arrayList

   } // end Try
   catch (NumberFormatException e) {
    System.err.println("ERROR!");
    main(null);

   } // end catch

  }// end for loop

 }

 public void displayPoints() {

  for (int i = 0; i < MAX_POINTS; i++) {

   JOptionPane.showMessageDialog(null, "Point number " + (i + 1)
     + " is: " + coordinateList.get(i));

  }

  // alt method
  // Iterator i = coordinateList.iterator();
  // String outputTemp;
  // while (i.hasNext()) {
  // outputTemp = i.next().toString();
  // JOptionPane.showMessageDialog(null, "Point number " + " is: "
  // + outputTemp);
  // }

 }


 /**
  * This sorts the points by the X coordinates
  */
  public void sortByXCoordinates(){

   coordinateList.sort(coordinates, new PointCompare());
  }

   public class PointCompare implements Comparator<Point> {

   public int compare(Point a, Point b) {
    if (a.x < b.x) {
     return -1;
    } else if (a.x > b.x) {
     return 1;
    } else {
     return 0;
    }
   }
   }

   public static void main(String[] args) {
  ConvexHullMain main = new ConvexHullMain();

  main.inputCoordinates();
  main.displayPoints();


 }
}
4

5 回答 5

6
private ArrayList<Point> coordinateList = new ArrayList<Point>();

...

Collections.sort(coordinateList, new PointCompare());

...

public class PointCompare implements Comparator<Point> {
    public int compare(Point a, Point b) {
        if (a.x < b.x) {
            return -1;
        }
        else if (a.x > b.x) {
            return 1;
        }
        else {
            return 0;
        }
    }
}
于 2010-11-16T22:13:05.100 回答
4

你很亲密。您遇到的问题只是您调用了

  public void sortByXCoordinates(){

   coordinateList.sort(coordinates, new PointCompare());

  }

你想要的是这样的:

import java.awt.Point;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import javax.swing.JOptionPane;

public class MainClass {

    private final Point coordinates = new Point(0, 0);
    private final int MAX_POINTS = 3;
    private final ArrayList<Point> coordinateList = new ArrayList<Point>();

    public void inputCoordinates() {

        String tempString;
        int tempx = 0;
        int tempy = 0;

        for (int i = 0; i < this.MAX_POINTS; i++) {
            try {
                tempString = JOptionPane.showInputDialog(null, "Enter X coordinate:");
                tempx = Integer.parseInt(tempString);
                tempString = JOptionPane.showInputDialog(null, "Enter Y coordinate:");
                tempy = Integer.parseInt(tempString);
                this.coordinates.setLocation(tempx, tempy);// set input data into
                this.coordinateList.add(this.coordinates.getLocation()); // put in
            }
            catch (final NumberFormatException e) {
                System.err.println("ERROR!");
                main(null);

            }
        }
    }

    public void displayPoints() {

        for (int i = 0; i < this.MAX_POINTS; i++) {

            JOptionPane.showMessageDialog(null, "Point number " + (i + 1) + " is: " + this.coordinateList.get(i));

        }

    }

    /**
     * This sorts the points by the X coordinates
     */
    public void sortByXCoordinates() {

        Collections.sort(this.coordinateList, new PointCompare());

    }

    public class PointCompare
        implements Comparator<Point> {

        public int compare(final Point a, final Point b) {
            if (a.x < b.x) {
                return -1;
            }
            else if (a.x > b.x) {
                return 1;
            }
            else {
                return 0;
            }
        }
    }

    public static void main(final String[] args) {
        final MainClass main = new MainClass();

        main.inputCoordinates();
        main.displayPoints();

    }
}
于 2010-11-16T22:14:49.530 回答
2

我将忽略您发布的所有代码,因为您只是丢弃了所有内容,而没有花时间识别相关区域。

现在,根据您的问题:您有一个ArrayList包含Points。您想按 X 轴/值对其进行排序。

List<Point> list = new ArrayList<Point>();

首先,您需要一个Comparator可以相互比较Point的。

Comparator<Point> comp = new Comparator<Point>()
{
    @Override
    public int compare(Point o1, Point o2)
    {
        return new Integer(o1.x).compareTo(o2.x);
    }
};

我选择将 int “装箱”为 Integer 并使用 Integer 的 compareTo 方法。你可以想出一个更整洁的比较方法,这取决于你。

然后您可以使用实用程序方法Collections.sort

Collections.sort(list, comp);

并且您的列表已排序。

于 2010-11-16T22:14:08.733 回答
0

我正在使用 Point Class 来管理 (x,y) 坐标列表,我需要按 X 的顺序对它们进行排序

您可以使用Bean Comparator或自定义 Comparator,如博客中所述。

于 2010-11-16T22:09:24.303 回答
0

您用于“坐标列表”的 ArrayList 类(请参阅 API 文档:http: //download.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html)没有排序( ) 方法。你必须自己实现它,或者使用 Collections.sort()。

于 2010-11-16T22:14:38.030 回答