我有一个家庭作业,并且遇到了一些麻烦。首先,任务是制作一个不同大小的条形图,然后通过每次单击按钮对其进行调整和排序。我在主类上实现了动作监听器,然后制作了一个辅助类来实现可比性。我在调用可比较函数时遇到问题。它说我的数组 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
}