在写一个排序算法演示程序
先在主窗体中的Listener中调用engine:
private class StartListener implements ActionListener{
public void actionPerformed(ActionEvent e){
int[] unsort=UnsortGenerator(); //函数返回给unsort一个数组
engine.setArray(unsort);
engine.setAlgorithm(getAlgorithm());
engine.setSleepTime(jslSpeed.getValue()*50);
engine.setHistogram(histogram); //Histogram是用来画柱状图的
engine.run();
}
}
然后在Engine中:
private SelectionSort selectionSort=new SelectionSort(); ... if (thread != null && thread.getState() != Thread.State.TERMINATED) return; thread=new Thread(selectionSort); selectionSort.setArray(unsort); thread.start();
在SelectionSort中:
public class SelectionSort implements Runnable{
private int[] unsort;
private Histogram histogram;
private int sleepTime;
public void run(){
selectionSort();
}
public void setArray(int[] unsort){
this.unsort=unsort;
}
private void selectionSort(){
int key=0;
int count=0;
while(countunsort[i])
key=i;
}
int temp=unsort[count];
unsort[count]=unsort[key];
unsort[key]=temp;
count++;
key=count;
for(int i=0;i

请问这段代码哪里有问题?如何修改?
提前感谢!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号
空指针异常在于histogram而不在于unsort
engine中只设置了array,再设置上histogram就可以了