java使用IO流对数组排序实例讲解
作者:小妮浅浅 时间:2023-09-04 02:24:19
在学会了java中io流的使用后,我们对于数组的排序,又多了一种使用方法。大家知道流处理数据的效率是比较理想的,那么在具体操作数组排序上,很多人对于排序的方法还没有明确。下面我们先java使用流对数组排序的思路为大家进行梳理,然后带来对应的实例代码方法。
1、排序思路
(1)从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取
(2)询问用户需要多少位数的数组
(3)转换为数字类型
(4)将用户输入数字存入数组
(5)把数组按排序需求并打印出来
2、实例
public static void main(String[] args) {
// TODO Auto-generated method stub
try {
//数组a()的数字个数,由用户输入决定
InputStreamReader isr=new InputStreamReader(System.in);
//从字符输入流中读取文本,缓冲各个字符,从而实现字符、数组和行的高效读取
BufferedReader bfr=new BufferedReader(isr);
//询问用户需要多少位数的数组
System.out.println("请输入需要多少位数的数组:\n");
String a1=bfr.readLine();
//将a1转换为数字类型
int i=Integer.parseInt(a1);
//提示用户输入数组数据
System.out.println("请向数组中存入"+i+"个数据:\n");
//将用户输入数字存入数组
Integer[] a=new Integer[i];
for(int j=0;j<i;j++){
System.out.println("第"+(j+1)+"个:");
a[j]=new Integer(bfr.readLine());
}
//把数组按升序排序并打印出来
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;
}
}
}
//输出排序后的数组
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个学生信息(姓名,语文成绩,数学成绩,英语成绩),按照总分从高到低存入文本文件。
代码:
public class TreeSetDemo {
public static void main(String[] args) throws IOException{
//创建TreeSet对象,用接口匿名内部类的方式实现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+"名学生的数学成绩:");
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);
}
//创建字符缓冲输出流对象
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();
}
}
来源:https://www.py.cn/java/shuzu/26777.html
标签:java,IO流,数组排序
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
浅谈Java 三种方式实现接口校验
2023-01-20 02:57:55
C#自定义繁体和简体字库实现中文繁体和简体之间转换的方法
2023-03-16 06:00:33
Springboot启动流程详细分析
2023-11-29 00:23:10
C#读写操作app.config中的数据应用介绍
2021-07-25 21:33:19
Java中lambda表达式的基本运用
2023-09-09 08:02:36
![](https://img.aspxhome.com/file/2023/4/83134_0s.png)
详解C#的排列组合
2021-06-05 15:40:11
![](https://img.aspxhome.com/file/2023/4/122664_0s.png)
Java CPU性能分析工具代码实例
2023-09-28 04:52:54
java异常处理机制示例(java抛出异常、捕获、断言)
2022-11-16 21:26:13
![](https://img.aspxhome.com/file/2023/2/71952_0s.jpg)
C#中单问号(?)和双问号(??)的用法整理
2021-08-27 02:50:53
Java如果在try里面执行return还会不会执行finally
2022-03-01 18:16:28
vue+springboot前后端分离工程跨域问题解决方案解析
2023-08-06 06:51:10
一篇文章带你搞定JAVA内存泄漏
2022-04-28 21:07:08
![](https://img.aspxhome.com/file/2023/8/132028_0s.jpg)
详解Java信号量Semaphore的原理及使用
2023-09-18 00:13:15
![](https://img.aspxhome.com/file/2023/8/104198_0s.jpg)
Spring整合Junit的使用详解
2022-11-20 18:33:17
分析并发编程之LongAdder原理
2023-05-11 17:19:30
![](https://img.aspxhome.com/file/2023/9/65599_0s.jpg)
浅谈C# StringBuilder内存碎片对性能的影响
2023-12-15 08:55:47
Java及nginx实现文件权限控制代码实例
2022-05-01 02:48:18
探讨:android项目开发 统筹兼顾 需要考虑的因素
2023-08-05 11:21:32
Android Application的使用全面解析
2023-08-26 04:23:49
![](https://img.aspxhome.com/file/2023/0/137520_0s.png)
Android亮屏速度分析总结
2023-11-06 11:55:21
![](https://img.aspxhome.com/file/2023/7/137777_0s.jpg)