黑马程序员技术交流社区

标题: 字节流与HashSet集合综合练习 [打印本页]

作者: _J2EE_LiXiZhen    时间: 2017-11-19 22:59
标题: 字节流与HashSet集合综合练习
使用集合存储10个1-50(含50)的随机偶数元素,
要求数字不能重复,按指定格式输出到根目录的num.txt中,
例如: 48,44,40,38,34,30,26......

[Java] 纯文本查看 复制代码
public class Test03 {
        public static void main(String[] args) throws IOException {
                HashSet<Integer> set = new HashSet<Integer>();
                Random r = new Random();
               
                while(set.size()<10) {
                        set.add(r.nextInt(50)+1);
                }
               
                ArrayList<Integer> list = new ArrayList<>();
                list.addAll(set);
               
                Collections.sort(list);
               
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("num.txt"));
               
                for (int i = list.size()-1;i>=0;i--) {
                        bos.write(list.get(i).toString().getBytes());
                        bos.write("\r\n".getBytes());
                        bos.flush();
                }
                bos.close();
        }
}





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2