importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.Font;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.InputEvent;importjava.awt.event.KeyAdapter;importjava.awt.event.KeyEvent;importjava.awt.event.MouseAdapter;importjava.awt.event.MouseEvent;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;importjava.io.BufferedReader;importjava.io.BufferedWriter;importjava.io.File;importjava.io.FileReader;importjava.io.FileWriter;importjava.io.IOException;importjavax.swing.BorderFactory;importjavax.swing.JFileChooser;importjavax.swing.JFrame;importjavax.swing.JLabel;importjavax.swing.JMenu;importjavax.swing.JMenuBar;importjavax.swing.JMenuItem;importjavax.swing.JOptionPane;importjavax.swing.JPopupMenu;importjavax.swing.JScrollPane;importjavax.swing.JTextArea;importjavax.swing.KeyStroke;importjavax.swing.ScrollPaneConstants;importjavax.swing.SwingConstants;publicclassJNotePadUIextendsJFrame{privateJMenuItemmenuOpen;privateJMenuItemmenuSave;privateJMenuItemmenuSaveAs;privateJMenuItemmenuClose;privateJMenueditMenu;privateJMenuItemmenuCut;privateJMenuItemmenuCopy;privateJMenuItemmenuPaste;privateJMenuItemmenuAbout;privateJTextAreatextArea;privateJLabelstateBar;privateJFileChooserfileChooser;privateJPopupMenupopUpMenu;publicJNotePadUI(){super("新建文本文件");setUpUIComponent();setUpEventListener();setVisible(true);}privatevoidsetUpUIComponent(){setSize(640,480);//菜單欄JMenuBarmenuBar=newJMenuBar();//設(shè)置「文件」菜單JMenufileMenu=newJMenu("文件");menuOpen=newJMenuItem("打開");//快捷鍵設(shè)置menuOpen.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_O,InputEvent.CTRL_MASK));menuSave=newJMenuItem("保存");menuSave.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_S,InputEvent.CTRL_MASK));menuSaveAs=newJMenuItem("另存為");menuClose=newJMenuItem("關(guān)閉");menuClose.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_Q,InputEvent.CTRL_MASK));fileMenu.add(menuOpen);fileMenu.addSeparator();//分隔線fileMenu.add(menuSave);fileMenu.add(menuSaveAs);fileMenu.addSeparator();//分隔線fileMenu.add(menuClose);//設(shè)置「編輯」菜單JMenueditMenu=newJMenu("編輯");menuCut=newJMenuItem("剪切");menuCut.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,InputEvent.CTRL_MASK));menuCopy=newJMenuItem("復(fù)制");menuCopy.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_C,InputEvent.CTRL_MASK));menuPaste=newJMenuItem("粘貼");menuPaste.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_V,InputEvent.CTRL_MASK));editMenu.add(menuCut);editMenu.add(menuCopy);editMenu.add(menuPaste);//設(shè)置「關(guān)于」菜單JMenuaboutMenu=newJMenu("關(guān)于");menuAbout=newJMenuItem("關(guān)于JNotePad");aboutMenu.add(menuAbout);menuBar.add(fileMenu);menuBar.add(editMenu);menuBar.add(aboutMenu);setJMenuBar(menuBar);//文字編輯區(qū)域textArea=newJTextArea();textArea.setFont(newFont("宋體",Font.PLAIN,16));textArea.setLineWrap(true);JScrollPanepanel=newJScrollPane(textArea,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);ContainercontentPane=getContentPane();contentPane.add(panel,BorderLayout.CENTER);//狀態(tài)欄stateBar=newJLabel("未修改");stateBar.setHorizontalAlignment(SwingConstants.LEFT);stateBar.setBorder(BorderFactory.createEtchedBorder());contentPane.add(stateBar,BorderLayout.SOUTH);popUpMenu=editMenu.getPopupMenu();fileChooser=newJFileChooser();}privatevoidsetUpEventListener(){//按下窗口關(guān)閉鈕事件處理addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){closeFile();}});//菜單-打開menuOpen.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){openFile();}});//菜單-保存menuSave.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){saveFile();}});//菜單-另存為menuSaveAs.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){saveFileAs();}});//菜單-關(guān)閉文件menuClose.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){closeFile();}});//菜單-剪切menuCut.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){cut();}});//菜單-復(fù)制menuCopy.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){copy();}});//菜單-粘貼menuPaste.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){paste();}});//菜單-關(guān)于menuAbout.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){//顯示對話框JOptionPane.showOptionDialog(null,"程序名稱:\nJNotePad\n"+"程序設(shè)計(jì):\n\n"+"簡介:\n一個簡單的文字編輯器\n"+"可作為驗(yàn)收J(rèn)ava的實(shí)現(xiàn)對象\n"+"歡迎網(wǎng)友下載研究交流\n\n"+"/","關(guān)于JNotePad",JOptionPane.DEFAULT_OPTION,JOptionPane.INFORMATION_MESSAGE,null,null,null);}});//編輯區(qū)鍵盤事件textArea.addKeyListener(newKeyAdapter(){publicvoidkeyTyped(KeyEvente){processTextArea();}});//編輯區(qū)鼠標(biāo)事件textArea.addMouseListener(newMouseAdapter(){publicvoidmouseReleased(MouseEvente){if(e.getButton()==MouseEvent.BUTTON3)popUpMenu.show(editMenu,e.getX(),e.getY());}publicvoidmouseClicked(MouseEvente){if(e.getButton()==MouseEvent.BUTTON1)popUpMenu.setVisible(false);}});}privatevoidopenFile(){if(isCurrentFileSaved()){//文件是否為保存狀態(tài)open();//打開}else{//顯示對話框intoption=JOptionPane.showConfirmDialog(null,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);switch(option){//確認(rèn)文件保存caseJOptionPane.YES_OPTION:saveFile();//保存文件break;//放棄文件保存caseJOptionPane.NO_OPTION:open();break;}}}privatebooleanisCurrentFileSaved(){if(stateBar.getText().equals("未修改")){returnfalse;}else{returntrue;}}privatevoidopen(){//fileChooser是JFileChooser的實(shí)例//顯示文件選取的對話框intoption=fileChooser.showDialog(null,null);//使用者按下確認(rèn)鍵if(option==JFileChooser.APPROVE_OPTION){try{//開啟選取的文件BufferedReaderbuf=newBufferedReader(newFileReader(fileChooser.getSelectedFile()));//設(shè)定文件標(biāo)題setTitle(fileChooser.getSelectedFile().toString());//清除前一次文件textArea.setText("");//設(shè)定狀態(tài)欄stateBar.setText("未修改");//取得系統(tǒng)相依的換行字符StringlineSeparator=System.getProperty("line.separator");//讀取文件并附加至文字編輯區(qū)Stringtext;while((text=buf.readLine())!=null){textArea.append(text);textArea.append(lineSeparator);}buf.close();}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"開啟文件失敗",JOptionPane.ERROR_MESSAGE);}}}privatevoidsaveFile(){//從標(biāo)題欄取得文件名稱Filefile=newFile(getTitle());//若指定的文件不存在if(!file.exists()){//執(zhí)行另存為saveFileAs();}else{try{//開啟指定的文件BufferedWriterbuf=newBufferedWriter(newFileWriter(file));//將文字編輯區(qū)的文字寫入文件buf.write(textArea.getText());buf.close();//設(shè)定狀態(tài)欄為未修改stateBar.setText("未修改");}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"寫入文件失敗",JOptionPane.ERROR_MESSAGE);}}}privatevoidsaveFileAs(){//顯示文件對話框intoption=fileChooser.showSaveDialog(null);//如果確認(rèn)選取文件if(option==JFileChooser.APPROVE_OPTION){//取得選擇的文件Filefile=fileChooser.getSelectedFile();//在標(biāo)題欄上設(shè)定文件名稱setTitle(file.toString());try{//建立文件file.createNewFile();//進(jìn)行文件保存saveFile();}catch(IOExceptione){JOptionPane.showMessageDialog(null,e.toString(),"無法建立新文件",JOptionPane.ERROR_MESSAGE);}}}privatevoidcloseFile(){//是否已保存文件if(isCurrentFileSaved()){//釋放窗口資源,而后關(guān)閉程序dispose();}else{intoption=JOptionPane.showConfirmDialog(null,"文件已修改,是否保存?","保存文件?",JOptionPane.YES_NO_OPTION,JOptionPane.WARNING_MESSAGE,null);switch(option){caseJOptionPane.YES_OPTION:saveFile();break;caseJOptionPane.NO_OPTION:dispose();}}}privatevoidcut(){textArea.cut();stateBar.setText("已修改");popUpMenu.setVisible(false);}privatevoidcopy(){textArea.copy();popUpMenu.setVisible(false);}privatevoidpaste(){textArea.paste();stateBar.setText("已修改");popUpMenu.setVisible(false);}privatevoidprocessTextArea(){stateBar.setText("已修改");}publicstaticvoidmain(String[]args){newJNotePadUI();}}
成都創(chuàng)新互聯(lián)公司是一家專業(yè)提供來安企業(yè)網(wǎng)站建設(shè),專注與網(wǎng)站設(shè)計(jì)、成都做網(wǎng)站、H5網(wǎng)站設(shè)計(jì)、小程序制作等業(yè)務(wù)。10年已為來安眾多企業(yè)、政府機(jī)構(gòu)等服務(wù)。創(chuàng)新互聯(lián)專業(yè)網(wǎng)絡(luò)公司優(yōu)惠進(jìn)行中。
給你介紹4種排序方法及源碼,供參考
1.冒泡排序
主要思路: 從前往后依次交換兩個相鄰的元素,大的交換到后面,這樣每次大的數(shù)據(jù)就到后面,每一次遍歷,最大的數(shù)據(jù)到達(dá)最后面,時間復(fù)雜度是O(n^2)。
public?static?void?bubbleSort(int[]?arr){
for(int?i?=0;?i??arr.length?-?1;?i++){
for(int?j=0;?j??arr.length-1;?j++){
if(arr[j]??arr[j+1]){
arr[j]?=?arr[j]^arr[j+1];
arr[j+1]?=?arr[j]^arr[j+1];
arr[j]?=?arr[j]^arr[j+1];
}
}
}
}
2.選擇排序
主要思路:每次遍歷序列,從中選取最小的元素放到最前面,n次選擇后,前面就都是最小元素的排列了,時間復(fù)雜度是O(n^2)。
public?static?void?selectSort(int[]?arr){
for(int?i?=?0;?i?arr.length?-1;?i++){
for(int?j?=?i+1;?j??arr.length;?j++){
if(arr[j]??arr[i]){
arr[j]?=?arr[j]^arr[i];
arr[i]?=?arr[j]^arr[i];
arr[j]?=?arr[j]^arr[i];
}
}
}
}
3.插入排序
主要思路:使用了兩層嵌套循環(huán),逐個處理待排序的記錄。每個記錄與前面已經(jīng)排好序的記錄序列進(jìn)行比較,并將其插入到合適的位置,時間復(fù)雜度是O(n^2)。
public?static?void?insertionSort(int[]?arr){
int?j;
for(int?p?=?1;?p??arr.length;?p++){
int?temp?=?arr[p];???//保存要插入的數(shù)據(jù)
//將無序中的數(shù)和前面有序的數(shù)據(jù)相比,將比它大的數(shù),向后移動
for(j=p;?j0??temp?arr[j-1];?j--){
arr[j]?=?arr[j-1];
}
//正確的位置設(shè)置成保存的數(shù)據(jù)
arr[j]?=?temp;
}
}
4.希爾排序
主要思路:用步長分組,每個分組進(jìn)行插入排序,再慢慢減小步長,當(dāng)步長為1的時候完成一次插入排序,? 希爾排序的時間復(fù)雜度是:O(nlogn)~O(n2),平均時間復(fù)雜度大致是O(n^1.5)
public?static?void?shellSort(int[]?arr){
int?j?;
for(int?gap?=?arr.length/2;?gap??0?;?gap/=2){
for(int?i?=?gap;?i??arr.length;?i++){
int?temp?=?arr[i];
for(j?=?i;?j=gap??temparr[j-gap];?j-=gap){
arr[j]?=?arr[j-gap];
}
arr[j]?=?temp;
}
}
}
你在輸出后面加個i就好了,循環(huán)到幾就輸出幾
for(i=1;i10;i++)
System.out.println(”Yuanzhen“+i);
}
網(wǎng)頁名稱:簡易的java源代碼 java源代碼怎么編譯
網(wǎng)站地址:http://jinyejixie.com/article28/ddoihjp.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、外貿(mào)網(wǎng)站建設(shè)、關(guān)鍵詞優(yōu)化、電子商務(wù)、品牌網(wǎng)站建設(shè)、網(wǎng)站制作
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)