import java.awt.BorderLayout
import java.awt.event.ActionEvent
import java.awt.event.ActionListener
import java.sql.Connection
import java.sql.DriverManager
import java.sql.ResultSet
import java.sql.SQLException
import java.sql.Statement
import javax.swing.BorderFactory
import javax.swing.JButton
import javax.swing.JFrame
import javax.swing.JPanel
import javax.swing.JScrollPane
import javax.swing.JTextArea
import javax.swing.JTextField
class Zidian extends JFrame implements ActionListener
{
private static final long serialVersionUID = 1L
JTextField txf = new JTextField (20)
JTextArea jt = new JTextArea ()
JButton btn1 = new JButton ("查询")
Zidian ()
{
setTitle ("Search")
setBounds (400, 300, 450, 350)
JPanel pnl4 = new JPanel ()
pnl4.setBorder (BorderFactory.createTitledBorder ("Search"))
pnl4.add (txf)
pnl4.add (btn1)
btn1.addActionListener (this)
JPanel pnl5 = new JPanel (new BorderLayout ())
pnl5.setBorder (BorderFactory.createTitledBorder ("Result"))
jt.setWrapStyleWord (true)
jt.setLineWrap (true)
pnl5.add (new JScrollPane (jt), BorderLayout.CENTER)
JPanel all = new JPanel ()
all.setLayout (new BorderLayout ())
all.add (pnl4, BorderLayout.NORTH)
all.add (pnl5, BorderLayout.CENTER)
add (all)
setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE)
setVisible (true)
}
public void actionPerformed ( ActionEvent e )
{
if (e.getSource () == btn1)
{
String str = txf.getText ()
String sql = "select * from chinese where charsound like '%" + str + "%'"
Connection conn = null
Statement stmt = null
ResultSet rs = null
try
{
Class.forName ("com.mysql.jdbc.Driver")
conn = DriverManager.getConnection ("jdbc:mysql://localhost:3306/japan?user=root&password=123")
stmt = conn.createStatement ()
rs = stmt.executeQuery (sql)
jt.setText ("")
while (rs.next ())
{
jt.append (new String (rs.getString ("charname").getBytes ("iso-8859-1"), "gb2312") + "\t")
}
}
catch (Exception eq)
{
eq.printStackTrace ()
}
finally
{
try
{
if (null != rs)
{
rs.close ()
}
if (null != stmt)
{
stmt.close ()
}
if (null != conn)
{
conn.close ()
}
}
catch (SQLException ignore)
{}
}
}
}
public static void main ( String[] args )
{
new Zidian ()
}
}
package special7_2import java.util.*
public class Special7_2 {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str=""
Scanner scan=new Scanner(System.in)
ArrayList<String>arraylist=new ArrayList<String>()
while(!str.equals("#")){
str=scan.nextLine()
arraylist.add(str)
}
arraylist.remove("#")
Object[]arr=arraylist.toArray()
for(int i=0i<arraylist.size()-1i++){
//String str1=(String)arr[i]
for(int j=0j<arraylist.size()-1-ij++){
if(((String) arr[j]).compareTo((String) arr[j+1])>0){
//swap(arr,i,j)
String temp
temp=(String) arr[j]
arr[j]=arr[j+1]
arr[j+1]=temp
}
}
}
for(int i=0i<arraylist.size()i++){
//String str1=(String)arr[i]
System.out.println(arr[i])
}
}
/*private static void swap(Object[] arra, int i, int j) {
// TODO Auto-generated method stub
String temp = (String) arra[i]
arra[i] = arra[j]
arra[j] = temp
}*/
}
import java.util.Arraysimport java.util.Scanner
//必须实现Comparable接口
public class Word implements Comparable<Word>{
private final String word
public String getWord() {
return word
}
//构造器什么的无视吧
public Word(String word) {
if(word == null)
throw new NullPointerException("不可以创造空单词!")
this.word = word
}
//实现compareTo方法.主要的排序思路在这里
@Override
public int compareTo(Word target) {
if(target == null)
return 1
if(target.getWord().equalsIgnoreCase(getWord()))
return 0
char[] selfLetters = getWord().toLowerCase().toCharArray()
char[] targetLetters = target.getWord().toLowerCase().toCharArray()
int selfLength = selfLetters.length
int targeLength =targetLetters.length
int minLength = Math.min(selfLength, targeLength)
for(int index = 0index < minLengthindex++){
if(selfLetters[index] > targetLetters[index]){
return 1
}
else if (selfLetters[index] < targetLetters[index]){
return -1
}
continue
}
return selfLength > targeLength ? 1 : -1
}
//重写 ToString 方法以便打印输出
@Override
public String toString() {
return word
}
//主方法.用来查看效果
public static void main(String[] args) {
int size = 5//测试用的数组长度(单词数)
// 创造一个Word的数组用来保存输入的单词
Word[] words = new Word[size]
Scanner sc = new Scanner(System.in)
for(int i=0i<sizei++)
{
System.out.println("请输入第"+(i+1)+"个单词")
words[i] = new Word(sc.nextLine())
}
sc.close()//关闭流
System.out.println("排序结果为:")
//使用Arrays.sort方法排序,sort对自动调用你的compareTo方法来比较
Arrays.sort(words)
//打印出结果
System.out.println(Arrays.toString(words))
}
}
这是我刚写的。测试结果还可以。逻辑可能不是很严谨 不过作为作业应该应付足够了
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)