Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
Note! The correct answer to use to sc.hasNext ()!
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()){
int n = sc.nextInt();
int m = sc.nextInt();
Set<Integer> ab = new TreeSet<>();
for(int i = 0; i < n; ++i) ab.add(sc.nextInt());
for(int i = 0; i < m; ++i) ab.add(sc.nextInt());
for(int mem : ab){
System.out.printf("%d ", mem);
}
}
}
}
If you do not sc.hasNext () on error
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int m = sc.nextInt();
Set<Integer> ab = new TreeSet<>();
for(int i = 0; i < n; ++i) ab.add(sc.nextInt());
for(int i = 0; i < m; ++i) ab.add(sc.nextInt());
for(int mem : ab){
System.out.printf("%d ", mem);
}
}
}
This test is to use input and output streams do? This test is really algorithm do?