标签:asn class iterator div highlight ali 描述 rgs java
输入多行,先输入随机整数的个数,再输入相应个数的整数
返回多行,处理后的结果
11 10 20 40 32 67 40 20 89 300 400 15
10 15 20 32 40 67 89 300 400
分别使用HashSet与TreeSet实现
import java.util.ArrayList; import java.util.Collections; import java.util.HashSet; import java.util.Iterator; import java.util.Scanner; public class M2 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); while (scanner.hasNext()) { HashSet<Integer> aList=new HashSet<Integer>(); int i=scanner.nextInt(); for (int j=0; j< i; j++) { aList.add(scanner.nextInt()); } Iterator iterator=aList.iterator(); ArrayList<Integer> n =new ArrayList<Integer>(); for (int j = 0; j < aList.size(); j++) { n.add((Integer) iterator.next()); } Collections.sort(n); for (int m = 0; m < aList.size(); m++) { System.out.println(n.get(m)); } } } }
while(sc.hasNext())不只一个输入
import java.util.Scanner; import java.util.TreeSet; public class M3 { public static void main(String[] args) { Scanner scanner=new Scanner(System.in); while (scanner.hasNext()) { TreeSet<Integer> aList=new TreeSet<Integer>(); int i=scanner.nextInt(); for (int j=0; j< i; j++) { aList.add(scanner.nextInt()); } for (Integer m : aList) { System.out.println(m); } } } }
标签:asn class iterator div highlight ali 描述 rgs java
原文地址:https://www.cnblogs.com/52circle/p/9009488.html