2012-11-06 76 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接口的对象。