如何在for循环中添加延迟?

时间:2016-09-04 15:37:47

标签: java insertion-sort

在这里,我尝试使用插入排序对整数数组进行排序,并根据排序机制逐步打印数组。我想在每个排序步骤之间有一点延迟。我尝试了Thread.sleep和其他一些方法,但无法找到解决方案。请帮助我,我是编程的初学者。

    public void insertionSort(int array[]){

        int j, k, key;

        for(j=1; j<array.length; j++){

            key = array[j];
            k = j-1;

            while(k>=0 && array[k]>key){
                array[k+1] = array[k];
                k = k-1;
            }
            array[k+1] = key;
            for(int i=0; i<array.length; i++){
                txtArOutput.append(String.valueOf(arr[i])+"\t");
            }   
            txtArOutput.append("\n"); 
            //delay
        }
    }

1 个答案:

答案 0 :(得分:0)

希望这种方式有所帮助

public class InsertionSortDemo extends JFrame {

    private JTextArea textArea;
    private JButton button;

    public InsertionSortDemo() {

        textArea = new JTextArea();

        JPanel panel = new JPanel(new BorderLayout());
        panel.add(textArea, BorderLayout.CENTER);

        button = new JButton("Run Simulation");
        button.addActionListener( e -> {

            button.setEnabled(false);
            textArea.setText("");

            new Thread( () -> {

                Random r = new Random();

                int [] array = {

                        r.nextInt(100),
                        r.nextInt(100),
                        r.nextInt(100),
                        r.nextInt(100),
                        r.nextInt(100),
                        r.nextInt(100),
                        r.nextInt(100),
                        r.nextInt(100),
                        r.nextInt(100),
                        r.nextInt(100),
                        r.nextInt(100),
                        r.nextInt(100),
                        r.nextInt(100),
                        r.nextInt(100),
                        r.nextInt(100),
                        r.nextInt(100),

                };
                insertionSort(array);

            } ).start();

        } );

        panel.add(button, BorderLayout.SOUTH);

        setContentPane(panel);
        setSize(300, 400);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater( () -> new InsertionSortDemo() );
    }

    public void insertionSort(int array[]){

        int j, k, key;

        for(j=1; j<array.length; j++){

            key = array[j];
            k = j-1;

            while(k>=0 && array[k]>key){
                array[k+1] = array[k];
                k = k-1;
//                print(array);
            }
            array[k+1] = key;

            print(array);

            sleep(100);

        }

        SwingUtilities.invokeLater( () -> button.setEnabled(true) );

    }

    private void print(int array[]) {

        SwingUtilities.invokeLater( () -> {

            for (int i : array) {
                textArea.append(i + ",");
            }
            textArea.append(System.lineSeparator());

        } );

    }

    private void sleep(long n) {
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
        }
    }

}