我有一项家庭作业,遇到一些麻烦。首先,任务是制作各种尺寸的条形图,然后单击按钮即可对其进行调整和排序。我在主类上实现了动作侦听器,然后使第二类实现了可比性。我在调用可比功能时遇到问题。它说我的数组int []无法以可比的方法来解析,而该方法正在寻找可比的[]任何帮助或技巧都将不胜感激。这是我的代码:

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

}

最佳答案

Comparable应该由您可能有理由与同类型的其他对象进行比较的类来实现。

因此,例如,如果您有一个需要排序的矩形条形图,则可以制作一个矩形类,其中包含矩形的高度,宽度和位置。现在,由于这是您组成的一个类,您将需要实现compareTo函数以评估哪个Rectangle大于或小于另一个矩形。

如果查看compareTo()规范http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Comparable.html,您将看到:

返回值:
负整数,零或正整数,因为此对象小于,等于或大于指定的对象。

因此,如果此对象小于传递给compareTo()的对象,则返回-,如果等于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();
    }
}


显然,我遗漏了很多东西,但这应该使您指向正确的方向。试一试,看看您会想到什么。编码愉快!

关于java - 可比的Java,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13243622/

10-13 08:37