java使用IO流對數(shù)組排序實例講解
在學會了java中io流的使用后,我們對于數(shù)組的排序,又多了一種使用方法。大家知道流處理數(shù)據(jù)的效率是比較理想的,那么在具體操作數(shù)組排序上,很多人對于排序的方法還沒有明確。下面我們先java使用流對數(shù)組排序的思路為大家進行梳理,然后帶來對應的實例代碼方法。
1、排序思路
(1)從字符輸入流中讀取文本,緩沖各個字符,從而實現(xiàn)字符、數(shù)組和行的高效讀取
(2)詢問用戶需要多少位數(shù)的數(shù)組
(3)轉換為數(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轉換為數(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個學生信息(姓名,語文成績,數(shù)學成績,英語成績),按照總分從高到低存入文本文件。
代碼:
public class TreeSetDemo {
public static void main(String[] args) throws IOException{
//創(chuàng)建TreeSet對象,用接口匿名內部類的方式實現(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);
//次要排序條件,當總成績相同時按學生姓名內容比較
int num2=(num1==0)?s2.getName().length()-s1.getName().length():num1;
return num2;
}
});
//鍵盤錄入學生信息
System.out.println("請輸入學生信息:");
for(int x=1;x<6;x++) {
Scanner sc=new Scanner(System.in);
System.out.print("請輸入第"+x+"名學生的姓名:");
String name=sc.nextLine();
System.out.print("請輸入第"+x+"名學生的語文成績:");
int chineseScore=sc.nextInt();
System.out.print("請輸入第"+x+"名學生的數(shù)學成績:");
int mathScore=sc.nextInt();
System.out.print("請輸入第"+x+"名學生的英語成績:");
int englishScore=sc.nextInt();
//將錄入的學生信息封裝到學生對象里
Student s=new Student();
s.setName(name);
s.setChineseScore(chineseScore);
s.setMathScore(mathScore);
s.setEnglishScore(englishScore);
//把學生對象添加到集合中
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();
}
//關閉流,釋放資源
bw.close();
}
}
到此這篇關于java使用IO流對數(shù)組排序實例講解的文章就介紹到這了,更多相關java如何使用流對數(shù)組排序內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
使用Backoff策略提高HttpClient連接管理的效率
這篇文章主要為大家介紹了Backoff策略提高HttpClient連接管理的效率使用解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-10-10
activemq整合springboot使用方法(個人微信小程序用)
這篇文章主要介紹了activemq整合springboot使用(個人微信小程序用),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2019-03-03

