结课作业,希望能浅浅的帮到一些人,不用像我一样苦写一个星期
这是一个1到1000000的文档建立,改一下包名和主类名就能运行了。如果有不同的意见,请赐教!
package 数据库建立;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
public class shuju {
public static void main(String[] args) throws IOException{
String path = "d:\shuju\shujuwendang.txt";
File file = new File(path);
if(!file.exists()){
file.getParentFile().mkdirs();
}
file.createNewFile();
// write
FileWriter fw = new FileWriter(file, true);
BufferedWriter bw = new BufferedWriter(fw);
for(int i=1;i<=1000000;i++) {
bw.write(i+" ");
}
bw.flush();
bw.close();
fw.close();
}
}
有点拉但是希望可以帮到一些人(像我一样抽到这个题目的人)
package 线程与查找; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.io.BufferedReader; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.concurrent.*; import javax.swing.*; import java.awt.*; class shuenxuchazhao implements Callable{ private int n; private ArrayList a = new ArrayList (); public shuenxuchazhao(int n,ArrayList a) { this.n=n; this.a=a; } public Integer call(){ int b = 0; for(int i=0;i<1000000 ;i++) { if(Integer.parseInt(a.get(i))==n) { b=n; break; } if(i==999999) { if(Integer.parseInt(a.get(i))!=n) { b=0; } } } return b; } } class zhebanchazhao implements Callable { private int n; private ArrayList a = new ArrayList (); public zhebanchazhao(int n,ArrayList a) { this.n=n; this.a=a; } public Integer call() { int fanhuizhi=0; int mid,low=0,high=999999; while(low<=high){ mid = (low+high)/2; if(Integer.parseInt(a.get(mid))==n){ fanhuizhi=n; break; }else if(Integer.parseInt(a.get(mid)) a = new ArrayList (); static String[] sc= new String[10]; public static void read() { String pathname = "D:\shuju\shujuwendang.txt"; try (FileReader reader = new FileReader(pathname); BufferedReader br = new BufferedReader(reader) ) { String line; while ((line = br.readLine()) != null) { String[] s =line.split(" "); for(int i=0;i<1000000;i++) { a.add(s[i]); } } } catch (IOException e) { e.printStackTrace(); } } public static String[] chazhaof(int n) { @SuppressWarnings("resource") long start = System.currentTimeMillis(); long start2 = System.currentTimeMillis(); ExecutorService pool =Executors.newFixedThreadPool(2); Future shuenxu=pool.submit(new shuenxuchazhao(n,a)); Future zheban=pool.submit(new zhebanchazhao(n,a)); int shuenxu1=0,zheban1=0; long start3 = System.currentTimeMillis(); while(shuenxu1==0) { try { if(shuenxu.isDone() && shuenxu1==0) { shuenxu1=shuenxu.get(); if(shuenxu1==0) { sc[0]="未查到:"; sc[1]=" "; shuenxu1=1; } else { sc[0]="已查到:";sc[1]=Integer.toString(shuenxu1); } } }catch(InterruptedException |ExecutionException e) { } } long end = System.currentTimeMillis(); sc[3] = Long.toString(end - start); while(zheban1==0) { try { if(zheban.isDone() && zheban1==0) { zheban1=zheban.get(); if(zheban1==0) { sc[4]="未查到:"; sc[5]=" "; zheban1=1; } else { sc[4]="已查到:";sc[5]=Integer.toString(zheban1); } } }catch(InterruptedException |ExecutionException e) { } } long end2 = System.currentTimeMillis(); sc[7] = Long.toString(end2 - start2-(end-start3)); sc[2]="运行时间(ms)"; sc[6]="运行时间(ms)"; return sc; } public static void main(String args[]) { JFrame f=new JFrame("利用线程比较查找算法的运行时间"); f.setSize(640,600); f.setLocationRelativeTo(null); f.getContentPane().setBackground(Color.white); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JButton b=new JButton("导入"); b.setSize(100,30); b.setLocation(250,100); b.setBackground(Color.white); ActionListener al=new daoru(); b.addActionListener(al); TextField txt=new TextField(); txt.setSize(250,30); txt.setLocation(170,50); txt.setColumns(100); txt.setFont(new Font("宋体",Font.BOLD,20)); txt.setBackground(Color.white); JButton d=new JButton("查询"); d.setSize(100,30); d.setLocation(250,150); d.setBackground(Color.white); d.addActionListener(new ActionListener(){ public void actionPerformed(ActionEvent e){ String a; a=txt.getText(); int n; n=Integer.parseInt(a); sc=chazhaof(n); new tanchuang(sc); } }); f.setLayout(null); f.add(b); f.add(d); f.add(txt); SwingUtilities.invokeLater(()->{ f.setVisible(true); }); } } class tanchuang extends JDialog{//结果弹窗 public tanchuang(String sc[]){ this.setBounds(700,400,500,300); this.setVisible(true); Container c = this.getContentPane(); c.setLayout(null); JPanel jp=new JPanel(); JTextArea j1=new JTextArea(sc[0],1,30); JTextArea j2=new JTextArea(sc[1],1,30); JTextArea j3=new JTextArea(sc[2],1,30); JTextArea j4=new JTextArea(sc[3],1,30); JTextArea j5=new JTextArea(sc[4],1,30); JTextArea j6=new JTextArea(sc[5],1,30); JTextArea j7=new JTextArea(sc[6],1,30); JTextArea j8=new JTextArea(sc[7],1,30); jp.setSize(500,500); jp.setLocation(10,10); jp.add(j1); jp.add(j2); jp.add(j3); jp.add(j4); jp.add(j5); jp.add(j6); jp.add(j7); jp.add(j8); c.add(jp); } }



