Java如何对Point对象的ArrayList进行排序

发布于 2024-10-02 19:08:29 字数 2376 浏览 0 评论 0原文

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

我在网上阅读以创建一个实现比较器的新类 PointCompare,但是我不确定这是如何实现的有效,因此我在 sortByXCooperatives 方法中遇到编译器错误。

非常感谢帮助,欢迎任何评论,提前致谢。 这是我的一些代码:

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();


 }
}

I'm using the Point Class to manage a list of (x,y) coordinates and I need to sort them in order of X.

I read online to make a new class PointCompare that implements Comparator, however I'm not sure how this works and therefore I have a compiler error in the sortByXCoordinates method.

Help would be appreciated a lot, and any comments are welcome, thanks in advance.
Here is some of my code:

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();


 }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

夕色琉璃 2024-10-09 19:08:29
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;
        }
    }
}
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;
        }
    }
}
泪意 2024-10-09 19:08:29

你很接近。您遇到的问题很简单,您调用了

  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();

    }
}

You were close. The problem you had was simply that you invoked

  public void sortByXCoordinates(){

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

  }

What you want is this:

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();

    }
}
好菇凉咱不稀罕他 2024-10-09 19:08:29

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

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

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

首先,您需要一个Comparator,它将一个Point与另一个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);

并对您的列表进行排序。

i'm going to ignore all of the code you posted because you've just dumped everything without taking the time to identify the relevant areas.

now, from your question: you have an ArrayList containing Points. You want to sort it by the X axis/value.

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

Firstly you need a Comparator which will compare one Point to another.

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

I choose to "box" the int to an Integer and use Integer's compareTo method. You could come up with a tidier method of comparison, up to you.

Then you can use the utility method Collections.sort

Collections.sort(list, comp);

and your list is sorted.

挖个坑埋了你 2024-10-09 19:08:29

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

您可以使用 Bean Comparator 或博客中描述的自定义比较器。

I'm using the Point Class to manage a list of (x,y) coordinates and I need to sort them in order of X

You can use a Bean Comparator or a custom Comparator as described in the blog.

舟遥客 2024-10-09 19:08:29

ArrayList 类(请参阅 API 文档:http://您用于“coordinateList”的 /download.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html) 没有 sort() 方法。您必须自己实现此功能,或使用 Collections.sort()。

The ArrayList class (see API documentation: http://download.oracle.com/javase/1.5.0/docs/api/java/util/ArrayList.html) that you use for your 'coordinateList' does not have a sort() method. You will have to implement this yourself, or use Collections.sort().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文