有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

可比Java

我有一个家庭作业,有点麻烦。首先,任务是制作一个不同大小的条形图,然后通过单击按钮对其进行调整和排序。我在主类上实现了action listener,然后在第二个类上实现了comparable。调用可比较函数时出现问题。它说我的数组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

}

共 (3) 个答案

  1. # 1 楼答案

    实现Comparable<T>接口取决于要排序的类的对象。实现的compareTo(T)方法可以委托给此类的实例字段,以确定对象顺序

    最初,类T的对象保存在一个集合List<T> list中。 使用可比较的界面,您可以通过两种方式对集合进行排序: Collections.sort(list);Set set = new TreeSet(list);。Collections类对原始列表进行排序,TreeSet(list)创建一个新的排序集合。对于这两种工作方式,列表中的对象必须实现可比较的接口

    所使用的排序算法是Collections类的mergesort,不能更改。收藏。sort()方法将对元素进行排序的任务委托给Arrays.sort(list.toArray())。在内部,Arrays类将对象强制转换为Comparable,并调用compareTo()方法来执行元素的比较

    因此,如果您对执行选择排序或插入排序感兴趣,那么可以遵循JDK策略。可以实现一个实现各种排序算法的类,该类将获取一组实现可比较接口的对象

  2. # 2 楼答案

    Comparable是一个接口,应该由一个可以排序的类来实现

    要实现Comparable,只需实现一个方法compareTo,该方法将给定对象与另一个对象进行比较

    如果有一个名为Foo的对象可以排序,那么Foo应该实现Comparable

    这允许您对Foo对象集合进行排序

    class Foo implements Comparable {
        Integer fooIndex;
    
        compareTo(Object otherObject) {
            Foo otherFoo = (Foo) otherObject;
            return this.fooIndex.compareTo(otherFoo.fooIndex);
        }
    }
    

    上面是一个简单的比较方法的例子

    注意,它不检查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);
        }
    }
    
  3. # 3 楼答案

    Compariable应该由类实现,您可能有一些理由与相同类型的其他对象进行比较

    例如,如果你有一个需要排序的矩形条形图,你可以制作一个包含矩形高度、宽度和位置的矩形类。现在,由于这是您创建的一个类,您需要实现compareTo函数来计算哪个矩形大于或小于另一个矩形

    如果查看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();
        }
    }
    

    显然我漏掉了很多东西,但这应该能让你找到正确的方向。玩一玩,看看你能想出什么。编码快乐