2012-11-06 74 views
1

我有一個家庭作業,並且有一點麻煩。首先,這項任務是製作各種大小的條形圖,然後每調整一次按鈕,就可以對其進行調整和排序。我在主類上實現了動作監聽器,然後做了一個輔助類來實現可比較的。我有一個調用可比較函數的問題。它說我的數組int []無法用可比較的方法解決,即尋找可比較的[]任何幫助或技巧將不勝感激。這裏是我的代碼:可比較的Java

import java.util.*; 
import java.applet.Applet; 
import java.awt.*; 
import java.awt.event.*; 

import javax.swing.*; 



public class TwoSorts extends Applet implements ActionListener 

{ 
private final int APPLET_WIDTH = 600; 
private final int APPLET_HEIGHT = 600; 
Button sort; 
Label sort_label; 
String pr_name; 
int[] random = new int[20]; 
int[] sorter = new int[20]; 


public void init() 

{ 

    sort = new Button("Sort"); 
    add(sort); 
    sort.addActionListener(this); 
    sort_label = new Label("Orange Selection/Black Bubble"); 
    add(sort_label); 
    randomGen(random); 
    sorter = random; 
    setBackground (Color.white); 
    setSize (APPLET_WIDTH, APPLET_HEIGHT); 
} 

private void randomGen (int...random) { 


    for (int i = 0; i < 20; i++){ 
     random [i] = (int) (20 +(Math.random()*300-20)); 
     } 
} 

public void paint(Graphics g) 
{ 
    for (int i = 0; i < 20; i++){ 


     g.setColor(Color.blue); 
     g.fillRect((int) (10 + (i*50)), 300, 50, ((random[i]))); 
     g.setColor(Color.black); 
     g.fillRect((int) (10 + (i*50)), 300, 25, (sorter[i])); 
    } 

    g.drawRect (20, 30, 130, 50); 
    sort.setLocation(0,220); 
    sort_label.setLocation(0,270); 
    sort_label.setSize(400,30); 
} 


class action extends TwoSorts implements Comparable { 


public void actionPerformed(ActionEvent arg0) { 


    selectionSort(random); 
    insertionSort (sort); 
    repaint; 

public static void selectionSort (Comparable[] random) { 

    int min; 
    Comparable temp; 

    for (int index = 0; index < random.length-1; index++) 
    { 
     min = index; 
     for (int scan = index+1; scan < random.length; scan++) 
      if (random[scan].compareTo(random[min]) < 0) 
       min = scan; 

     temp = random[min]; 
     random[min] = random[index]; 
     random[index] = temp; 
    } 

public static void insertionSort (Comparable[] sorter) { 

    for (int index = 1; index < sorter.length; index ++){ 
     Comparable key = sorter[index]; 
     int position = index; 
     while (position > 0 && key.compareTo(sorter[position-1]) < 0){ 
      sorter [position] = sorter[position-1]; 
      position--; 
     } 

     sorter[position] = key; 
    } 
} 

@Override 
public int compareTo(Object o) { 
    // TODO Auto-generated method stub 
    return 0; 
    } 
} 


@Override 
public void actionPerformed(ActionEvent e) { 
    // TODO Auto-generated method stub 

} 
+1

你看起來很糟糕。實現Comparable接口的類不應該是進行排序的類,我懷疑應該是一個GUI類。只有要排序的非GUI數據類應該實現Comparable。此外,你的compareTo方法應該返回一個有意義的整數,而不是0,這會使你的類完全不可比。 –

+0

我只是非常困惑的整理排序的東西 – blankwall

+1

你看了這個教程?如果你試圖在你走的時候補上它,那對你來說會非常沮喪。你知道Integer實現了'Comparable ',如果你的數據是一個簡單的'ArrayList ',你可以調用'Collections.sort(...)',而不必擔心實現Comparable接口。 –

回答

4

可比應該與你可能有一些原因,與同類型的其它對象比較類實現。例如,如果您有需要排序的矩形條形圖,則可以製作一個包含矩形高度,寬度和位置的矩形類。現在,因爲這是你創建的類,你將需要實現compareTo函數來評估哪個Rectangle大於或小於另一個矩形。

如果你看的compareTo()規範http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Comparable.html您將看到:

返回: 負整數,零或正整數,根據此對象是小於,等於或大於指定的目的。

所以如果這個對象小於傳遞給compareTo()的對象return - ,如果等於0,那麼+如果更大。

考慮到這可能會最終有一個類,看起來像這樣

public class MyRect implements Comparable { 
    int width;  //width of the rectangle will probably not change 
    int height;  //this might be the value you want to compare in compareTo() 
    point position; 

    ... 

    //getters and setters yada yada 
    public int getHeight(){ 
     return this.height; 
    } 

    ... 

    @Override 
    public int compareTo(Object otherRect){ 

     // if this rectangle's height is greater than otherRect the difference should 
     // be positive, if equal 0, and if less than the difference will be negative 
     // exactly as specification for compareTo() states. 

     return this.height - (MyRect)otherRect.getHeight(); 
    } 
} 

顯然我已經留下了很多了,但應該讓你在正確的方向。玩弄它,看看你想出了什麼。快樂的編碼!

+0

非常感謝我會盡力讓它起來並去 – blankwall

+0

yw,我編輯了上面的代碼來糾正一些語法錯誤,並使它更清楚一點實際上可能會實施。希望能幫助到你。 –

+0

我得到了它的基本知識,但我的主要問題是如何讓我的數組隨機[]進入方法selectionSort(Comparable []) – blankwall

1

ComparablË是應該由可以排序的類來實現的接口。

要實現可比您只需執行一個方法compareTo,其給定的比較對象到另一個對象。

如果您有一個對象名爲Foo可以排序,Foo應該實現Comparable。

這使您可以對Foo對象的集合進行排序。

class Foo implements Comparable { 
    Integer fooIndex; 

    compareTo(Object otherObject) { 
     Foo otherFoo = (Foo) otherObject; 
     return this.fooIndex.compareTo(otherFoo.fooIndex); 
    } 
} 

上面是簡單的compareTo 方法的例子。

請注意,它不會檢查null,或者檢查是否有可能轉換爲Foo。

以上實現允許你這樣做:

List<Foo> fooList = createFooList(); 
Collections.sort(fooList); 

更妙的是,你可以實現一個類型可比接口(可能比較混亂)。

這可以讓你避免鑄造:

Class Foo implements Comparable<Foo> { 
    Integer fooIndex; 

    compareTo(Foo otherFoo) { 
     return this.fooIndex.compareTo(otherFoo.fooIndex); 
    } 
} 
0

實現Comparable<T>接口依賴於您想要排序的類的對象。 compareTo(T)方法的實現可以委託給這個類的實例字段來確定對象的排序。

最初,T類的對象保存在集合List<T> list中。 使用Comparable界面,您可以通過兩種方式對集合進行分類: Collections.sort(list);Set set = new TreeSet(list);。 Collections類對原始列表進行排序,TreeSet(list)創建一個新的排序集合。對於這兩種工作方式,列表中的對象必須實現Comparable接口。

使用的排序算法是Collections類的mergesort,它不能被更改。 Collections.sort()方法將排序元素的任務委託給Arrays.sort(list.toArray())。在內部,Arrays類將對象轉換爲Comparable並調用compareTo()方法來執行元素的比較。

因此,如果您有興趣進行選擇排序或插入排序,那麼您可以遵循JDK策略。實現各種排序算法的類可以實現,它將採用一組實現Comparable接口的對象。