eclipse软件
win7系统
1.插入排序法:将记录插入到有序数列中,就是把我们未排序的数据插入到已经排好的数列中,得到的序列仍然是有序的。其基本操作流程,见下图。
2.建立一个java工程:打开自己的变成软件eclipse软件,并且建立一个java工程,工程的目录层次如图所示,还有初始代码。
1.类的模型:根据我们要实现的任务可是,该类最基本需要包含以下方法,构造方法、数据获取方法,事件处理方法和main方法,以及一些属性,具体的属性代码如下: private JPanel jp = new JPanel(); private JButton jb_act = new JButton('排序'); private JButton jb_cle = new JButton('清空'); private JLabel jl = new JLabel('请输入数组,字符用空格隔开:'); private JLabel jl2 = new JLabel('排序结果如下:'); private JTextArea jta_in = new JTextArea(); private JTextArea jta_out = new JTextArea(); private JScrollPane jsp_in = new JScrollPane(jta_in); private JScrollPane jsp_out = new JScrollPane(jta_out); double[] array;
2.构造方法:该方法主要用于对我们的属性进行处理和界面的初始化,具体代码如下: public InsertSort() { jp.setLayout(null); jl.setBounds(30, 10, 340, 36); jp.add(jl); jl2.setBounds(30, 130, 100, 30); jp.add(jl2); jsp_in.setBounds(30, 45, 340, 70); jp.add(jsp_in); jta_in.setLineWrap(true); jsp_out.setBounds(30, 165, 340, 70); jp.add(jsp_out); jta_out.setLineWrap(true); jb_act.setBounds(200, 125, 70, 30); jp.add(jb_act); jb_cle.setBounds(300, 125, 70, 30); jp.add(jb_cle); jb_act.addActionListener(this); jb_cle.addActionListener(this); this.add(jp); this.setTitle('插入法排序'); this.setBounds(100, 100, 400, 300); this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.setVisible(true); this.setResizable(false); }
3.数据获取:该部分的代码用来获取窗口输入的数据,将窗口文本框中的字符串编程数组,具体代码如下:public void getArray() { String arrayStr = jta_in.getText().trim(); if (arrayStr.equals('')) { JOptionPane.showMessageDialog(null, '请输入数字内容'); return; } for (int i = 0; i < arrayStr.length(); i++) { char charAt = arrayStr.charAt(i); if (!Character.isDigit(charAt) && (charAt != ' ') && (charAt != '.')) { JOptionPane.showMessageDialog(null, '输入包含非数字内容'); jta_in.setText(''); return; } } String[] numStrs = arrayStr.split(' {1,}'); double[] numArray = new double[numStrs.length]; for (int i = 0; i < numArray.length; i++) { String[] temp = numStrs[i].split('.{1,}'); char[] tempstrs = numStrs[i].toCharArray(); if ((tempstrs[0] == '.') || (tempstrs[tempstrs.length - 1] == '.') || (temp.length >= 3)) { JOptionPane.showMessageDialog(null, '输入包含非数字内容'); jta_in.setText(''); return; } numArray[i] = Double.valueOf(numStrs[i]); } array = numArray; }
4.事件处理方法:由于在构造方法中添加了,两个按钮事件,我们需要对按钮事件进行处理,排序算法就是在其中实现的,具体代码如下:public void actionPerformed(ActionEvent e) { if (e.getSource() == jb_act) { getArray(); double temp; int j; for(int i=1;i
5.main方法:该实例的的main方法,具体代码如下: public static void main(String[] args) { new InsertSort(); }
1.编译运行:单击工具栏中的编译和运行按钮,我们就会看到如下图所示的界面了。
2.验证:接着我们在相应的输入框中输入我们要排序的数组,然后单击“排序”按钮,具体效果如下。
如有疑问可以留言
分享可以使我们懂得更多