2010-11-16 53 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