java使用IO流對數(shù)組排序?qū)嵗v解
在學(xué)會了java中io流的使用后,我們對于數(shù)組的排序,又多了一種使用方法。大家知道流處理數(shù)據(jù)的效率是比較理想的,那么在具體操作數(shù)組排序上,很多人對于排序的方法還沒有明確。下面我們先java使用流對數(shù)組排序的思路為大家進行梳理,然后帶來對應(yīng)的實例代碼方法。
1、排序思路(1)從字符輸入流中讀取文本,緩沖各個字符,從而實現(xiàn)字符、數(shù)組和行的高效讀取
(2)詢問用戶需要多少位數(shù)的數(shù)組
(3)轉(zhuǎn)換為數(shù)字類型
(4)將用戶輸入數(shù)字存入數(shù)組
(5)把數(shù)組按排序需求并打印出來
2、實例public static void main(String[] args) { // TODO Auto-generated method stub try { //數(shù)組a()的數(shù)字個數(shù),由用戶輸入決定 InputStreamReader isr=new InputStreamReader(System.in); //從字符輸入流中讀取文本,緩沖各個字符,從而實現(xiàn)字符、數(shù)組和行的高效讀取 BufferedReader bfr=new BufferedReader(isr); //詢問用戶需要多少位數(shù)的數(shù)組 System.out.println('請輸入需要多少位數(shù)的數(shù)組:n'); String a1=bfr.readLine(); //將a1轉(zhuǎn)換為數(shù)字類型 int i=Integer.parseInt(a1); //提示用戶輸入數(shù)組數(shù)據(jù) System.out.println('請向數(shù)組中存入'+i+'個數(shù)據(jù):n'); //將用戶輸入數(shù)字存入數(shù)組 Integer[] a=new Integer[i]; for(int j=0;j<i;j++){ System.out.println('第'+(j+1)+'個:'); a[j]=new Integer(bfr.readLine()); } //把數(shù)組按升序排序并打印出來 for(int k=1;k<i;k++){ for(int m=0;m<(i-k);m++){ if(a[m]>a[m+1]){ //Integer temp=new Integer(0); int temp=0; temp=a[m]; a[m]=a[m+1]; a[m+1]=temp; } } } //輸出排序后的數(shù)組 System.out.println('排序后n'); for(int t=0;t<=i;t++){ System.out.println(a[t]); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } } }
實例擴展:
鍵盤錄入5個學(xué)生信息(姓名,語文成績,數(shù)學(xué)成績,英語成績),按照總分從高到低存入文本文件。
代碼:
public class TreeSetDemo { public static void main(String[] args) throws IOException{ //創(chuàng)建TreeSet對象,用接口匿名內(nèi)部類的方式實現(xiàn)Comparator接口 TreeSet<Student> ts=new TreeSet<Student>(new Comparator<Student>() { //重寫Comparator接口中的compare()方法 @Override public int compare(Student s1,Student s2) { //主要排序條件:總成績,按從高到低輸出 int num1=s2.sum(s2)-s1.sum(s1); //次要排序條件,當(dāng)總成績相同時按學(xué)生姓名內(nèi)容比較 int num2=(num1==0)?s2.getName().length()-s1.getName().length():num1;return num2; }}); //鍵盤錄入學(xué)生信息 System.out.println('請輸入學(xué)生信息:'); for(int x=1;x<6;x++) { Scanner sc=new Scanner(System.in); System.out.print('請輸入第'+x+'名學(xué)生的姓名:'); String name=sc.nextLine(); System.out.print('請輸入第'+x+'名學(xué)生的語文成績:'); int chineseScore=sc.nextInt(); System.out.print('請輸入第'+x+'名學(xué)生的數(shù)學(xué)成績:'); int mathScore=sc.nextInt(); System.out.print('請輸入第'+x+'名學(xué)生的英語成績:'); int englishScore=sc.nextInt(); //將錄入的學(xué)生信息封裝到學(xué)生對象里 Student s=new Student(); s.setName(name); s.setChineseScore(chineseScore); s.setMathScore(mathScore); s.setEnglishScore(englishScore); //把學(xué)生對象添加到集合中 ts.add(s); } //創(chuàng)建字符緩沖輸出流對象 BufferedWriter bw=new BufferedWriter(new FileWriter('18-1.txt')); //遍歷 for(Student s:ts) { //利用StringBuffer中的追加功能,將需要輸出的信息集合在一起 StringBuffer sb=new StringBuffer(); sb.append(s.getName()).append(',').append(s.getChineseScore()).append(',').append(s.getMathScore()) .append(',').append(s.getEnglishScore()).append(',').append(s.sum(s)); //將信息寫入文本文件中 bw.write(sb.toString()); //換行 bw.newLine(); //刷新流 bw.flush(); } //關(guān)閉流,釋放資源 bw.close(); }}
到此這篇關(guān)于java使用IO流對數(shù)組排序?qū)嵗v解的文章就介紹到這了,更多相關(guān)java如何使用流對數(shù)組排序內(nèi)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. python利用os模塊編寫文件復(fù)制功能——copy()函數(shù)用法2. php測試程序運行速度和頁面執(zhí)行速度的代碼3. php網(wǎng)絡(luò)安全中命令執(zhí)行漏洞的產(chǎn)生及本質(zhì)探究4. 三個不常見的 HTML5 實用新特性簡介5. 無線標記語言(WML)基礎(chǔ)之WMLScript 基礎(chǔ)第1/2頁6. ajax請求添加自定義header參數(shù)代碼7. Python使用jupyter notebook查看ipynb文件過程解析8. 解決Python 進程池Pool中一些坑9. 解決python腳本中error: unrecognized arguments: True錯誤10. IntelliJ IDEA創(chuàng)建普通的Java 項目及創(chuàng)建 Java 文件并運行的教程
