2010-11-16 55 views
0

我使用的是点类管理的(X,Y)坐标列表,我需要他们在十的Java如何排序点的ArrayList对象

我看了网上做的排序顺序一个实现比较器的新类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

你很近。你有问题只是你调用

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

    } 
} 
0

我使用的是点类管理的(X,Y)坐标的列表,我需要他们的X进行排序

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

+0

非常感谢你。这可以帮助我编写一个冒泡排序来手动排序! – user492837 2010-11-18 12:50:56

+0

>使用冒泡排序。一次也没有。 – easymoden00b 2015-07-13 14:09:43

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

我要去忽略所有你贴,因为你刚刚甩了一切,而不需要花时间的代码确定相关领域。

现在,从您的问题:您有一个ArrayList包含Point s。你想通过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); 
    } 
}; 

我选择将box整型为Integer并使用Integer的compareTo方法。你可以想出一个比较整洁的比较方法,取决于你。

然后你就可以使用工具方法Collections.sort

Collections.sort(list, comp); 

,你的列表进行排序。

+0

哈哈我得到了一个downvote这个... – pstanton 2012-03-25 23:19:03