数组反转算法

来源:互联网 发布:交大网络教育学院登录 编辑:程序博客网 时间:2024/06/15 10:29
/* (程序头部注释开始)* 程序的版权和版本声明部分* Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved.* 文件名称:                              * 作    者:   臧鹏               * 完成日期:   2013   年 8月 16日* 版 本 号:      001    * 对任务及求解方法的描述部分* 输入描述: * 问题描述:利用eclipse的windowBuilder的编写的窗口程序.数组反转算法* 程序输出: * 程序头部的注释结束*/package 第三章数组;import java.awt.BorderLayout;public class 用数组翻转字符串 extends JFrame {private JPanel contentPane;private JTextField textField;private JTextField textField_1;private JTextArea textArea ;/** * Launch the application. */public static void main(String[] args) {EventQueue.invokeLater(new Runnable() {public void run() {try {用数组翻转字符串 frame = new 用数组翻转字符串();frame.setVisible(true);} catch (Exception e) {e.printStackTrace();}}});}/** * Create the frame. */public 用数组翻转字符串() {setTitle("\u7528\u6570\u7EC4\u53CD\u8F6C\u5B57\u7B26\u4E32");setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setBounds(100, 100, 450, 300);contentPane = new JPanel();contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));setContentPane(contentPane);contentPane.setLayout(null);JLabel lblNewLabel = new JLabel("\u8F93\u5165\u4E00\u4E2A\u5B57\u7B26\u4E32");lblNewLabel.setBounds(10, 33, 95, 15);contentPane.add(lblNewLabel);textField = new JTextField();textField.setBounds(139, 30, 285, 21);contentPane.add(textField);textField.setColumns(10);textField_1 = new JTextField();textField_1.setColumns(10);textField_1.setBounds(139, 64, 285, 21);contentPane.add(textField_1);JButton button = new JButton("\u53CD\u8F6C");button.addActionListener(new ActionListener() {public void actionPerformed(ActionEvent e) {String inputstr = textField.getText();      //获取用户输入的字符串char[] str = inputstr.toCharArray();         //转化为字符数组textArea.setText(""); //文本域清空for(int i = 0;i<str.length/2;i++){  //字符交换算法char temp = str[i];str[i] = str[str.length-i-1];str[str.length-i-1] = temp;textArea.append("第"+(i+1)+"次循环:\t"); //显示循环反转过程for(char c : str){                      //显示每次反转的结果textArea.append(c+"");}textArea.append("\n");                //文本域换行}String outputstr = new String(str);     //把字符数组转化为字符串textField_1.setText(outputstr);        //显示反转后的字符串}});button.setBounds(10, 63, 93, 23);contentPane.add(button); textArea = new JTextArea();textArea.setBounds(10, 96, 414, 156);contentPane.add(textArea);}}

原创粉丝点击