欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java使用選擇排序法對數(shù)組排序?qū)崿F(xiàn)代碼

 更新時間:2014年02月18日 16:05:26   作者:  
這篇文章主要介紹了Java使用選擇排序法對數(shù)組排序?qū)崿F(xiàn)代碼,需要的朋友可以參考下

編寫程序,實現(xiàn)將輸入的字符串轉(zhuǎn)換為一維數(shù)組,并使用選擇排序法對數(shù)組進(jìn)行排序。

思路如下:

點擊"生成隨機數(shù)"按鈕,創(chuàng)建Random隨機數(shù)對象;
使用JTextArea的setText()方法清空文本域;
創(chuàng)建一個整型一維數(shù)組,分配長度為10的空間;
初始化數(shù)組元素,使用Random類的nextInt()方法生成50以內(nèi)的隨機數(shù),使用JTextArea類的append()方法把數(shù)組元素顯示在文本域控件中;
點擊"排序"按鈕,使用JTextArea類的setText()方法清空文本域;
使用雙層for循環(huán),對從第二個元素到最后一個元素的每一趟排序,對該趟排序所涉及的元素進(jìn)行遍歷,查找最大值對應(yīng)的數(shù)組下標(biāo);
交換在位置array.length-i和index(最大值)兩個數(shù),使得每趟排序后找到的最大值都在該趟排序所涉及的數(shù)列的最后;
使用for循環(huán)遍歷數(shù)組,使用Random類的append方法把排序后的數(shù)組元素顯示到文本域中。
代碼如下:

復(fù)制代碼 代碼如下:

import java.awt.EventQueue;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.border.EmptyBorder;

public class SelectSort extends JFrame {

    /**
     *
     */
    private static final long serialVersionUID = 6824538613659403529L;
    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    SelectSort frame = new SelectSort();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the frame.
     */
    public SelectSort() {
        setTitle("使用選擇排序法對數(shù)組排序");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 300);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        setContentPane(contentPane);
        GridBagLayout gbl_contentPane = new GridBagLayout();
        gbl_contentPane.columnWidths = new int[] { 0, 0 };
        gbl_contentPane.rowHeights = new int[] { 0, 0, 0, 0, 0 };
        gbl_contentPane.columnWeights = new double[] { 1.0, Double.MIN_VALUE };
        gbl_contentPane.rowWeights = new double[] { 1.0, 0.0, 1.0, 0.0,
                Double.MIN_VALUE };
        contentPane.setLayout(gbl_contentPane);

        JScrollPane scrollPane = new JScrollPane();
        GridBagConstraints gbc_scrollPane = new GridBagConstraints();
        gbc_scrollPane.insets = new Insets(0, 0, 5, 0);
        gbc_scrollPane.fill = GridBagConstraints.BOTH;
        gbc_scrollPane.gridx = 0;
        gbc_scrollPane.gridy = 0;
        contentPane.add(scrollPane, gbc_scrollPane);

        textArea1 = new JTextArea();
        scrollPane.setViewportView(textArea1);

        JButton button = new JButton("生成隨機數(shù)");
        button.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_actionPerformed(e);
            }
        });
        GridBagConstraints gbc_button = new GridBagConstraints();
        gbc_button.insets = new Insets(0, 0, 5, 0);
        gbc_button.gridx = 0;
        gbc_button.gridy = 1;
        contentPane.add(button, gbc_button);

        JScrollPane scrollPane_1 = new JScrollPane();
        GridBagConstraints gbc_scrollPane_1 = new GridBagConstraints();
        gbc_scrollPane_1.insets = new Insets(0, 0, 5, 0);
        gbc_scrollPane_1.fill = GridBagConstraints.BOTH;
        gbc_scrollPane_1.gridx = 0;
        gbc_scrollPane_1.gridy = 2;
        contentPane.add(scrollPane_1, gbc_scrollPane_1);

        textArea2 = new JTextArea();
        scrollPane_1.setViewportView(textArea2);

        JButton button_1 = new JButton("排序");
        button_1.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_button_1_actionPerformed(e);
            }
        });
        GridBagConstraints gbc_button_1 = new GridBagConstraints();
        gbc_button_1.gridx = 0;
        gbc_button_1.gridy = 3;
        contentPane.add(button_1, gbc_button_1);
    }

    private int[] array = new int[10];
    private JTextArea textArea1;
    private JTextArea textArea2;

    protected void do_button_actionPerformed(ActionEvent e) {
        Random random = new Random();// 創(chuàng)建隨機數(shù)對象
        textArea1.setText("");// 清空文本域
        for (int i = 0; i < array.length; i++) {// 初始化數(shù)組元素
            array[i] = random.nextInt(50);// 生成50以內(nèi)的隨機數(shù)
            textArea1.append(array[i]+"  ");// 把數(shù)組元素顯示的文本域控件中
        }
    }

    protected void do_button_1_actionPerformed(ActionEvent e) {
        textArea2.setText("");// 清空文本域
        int index;
        for (int i = 1; i < array.length; i++) {
            index = 0;
            for (int j = 1; j <= array.length - i; j++) {
                if (array[j] > array[index]) {
                    index = j;// 查找最大值
                }
            }
            // 交換在位置array.length-i和index(最大值)兩個數(shù)
            int temp = array[array.length - i];
            array[array.length - i] = array[index];
            array[index] = temp;
        }
        for (int i = 0; i < array.length; i++) {
            textArea2.append(array[i] + "  ");// 把排序后的數(shù)組元素顯示到文本域中
        }
    }
}

效果如圖:

相關(guān)文章

  • 淺談springboot自動配置原理

    淺談springboot自動配置原理

    這篇文章主要介紹了淺談springboot自動配置原理,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-08-08
  • 從0到1構(gòu)建springboot web應(yīng)用鏡像并使用容器部署的過程

    從0到1構(gòu)建springboot web應(yīng)用鏡像并使用容器部署的過程

    這篇文章主要介紹了從0到1構(gòu)建springboot web應(yīng)用鏡像并使用容器部署,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • SpringBoot整合微信登錄功能的實現(xiàn)方案

    SpringBoot整合微信登錄功能的實現(xiàn)方案

    今天通過本文給大家分享微信登錄與SpringBoot整合過程,微信掃描登錄實現(xiàn)代碼知道掃描后點擊登錄的全部過程,本文給大家介紹的非常詳細(xì),需要的朋友可以參考下
    2021-10-10
  • 如何在SpringBoot中使用logback優(yōu)化異常堆棧的輸出詳解

    如何在SpringBoot中使用logback優(yōu)化異常堆棧的輸出詳解

    最近項目中整合了logback,所以下面這篇文章主要給大家介紹了關(guān)于如何在SpringBoot中使用logback優(yōu)化異常堆棧的輸出,文中通過實例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-05-05
  • springboottest測試依賴和使用方式

    springboottest測試依賴和使用方式

    這篇文章主要介紹了springboottest測試依賴和使用方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-03-03
  • Java中Calendar時間操作常用方法詳解

    Java中Calendar時間操作常用方法詳解

    這篇文章主要為大家詳細(xì)介紹了Java中Calendar時間操作常用方法,calendar中set方法和靜態(tài)屬性帶來的一些坑,感興趣的小伙伴們可以參考一下
    2016-05-05
  • spring boot實現(xiàn)在request里解密參數(shù)返回

    spring boot實現(xiàn)在request里解密參數(shù)返回

    這篇文章主要介紹了Spring Boot實現(xiàn)在request里解密參數(shù)返回操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java編寫ftp下載工具

    java編寫ftp下載工具

    本文給大家介紹的是如何一步步實現(xiàn)使用java編寫FTP下載工具,而且是在Linux環(huán)境下使用javac編譯的,在運行和編譯上有些不同之處,有需要的小伙伴們參考下吧。
    2015-03-03
  • Spring實現(xiàn)處理跨域請求代碼詳解

    Spring實現(xiàn)處理跨域請求代碼詳解

    這篇文章主要介紹了Spring實現(xiàn)處理跨域請求代碼詳解,具有一定借鑒價值,需要的朋友可以了解下。
    2017-12-12
  • SpringBoot2.x配置多數(shù)據(jù)源方式

    SpringBoot2.x配置多數(shù)據(jù)源方式

    這篇文章主要介紹了SpringBoot2.x配置多數(shù)據(jù)源方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-03-03

最新評論