java题目 整型数组合并
描述
题目标题:
将两个整型数组按照升序合并,并且过滤掉重复数组元素。
输出时相邻两数之间没有空格。
请注意本题有多组样例。
输入描述:
输入说明,按下列顺序输入:
1 输入第一个数组的个数
2 输入第一个数组的数值
3 输入第二个数组的个数
4 输入第二个数组的数值
输出描述:
输出合并之后的数组
示例1
输入:
3 1 2 5 4 -1 0 3 2
输出:
-101235
1 import java.util.*; 2 3 public class Main { 4 public static void main(String[] args) { 5 Scanner sc = new Scanner(System.in); 6 7 while(sc.hasNext()) { 8 int first = sc.nextInt(); 9 //Set是一种不包含重复的元素的Collection 10 TreeSet<Integer> set = new TreeSet<>(); 11 for(int i =0; i < first; i++) { 12 set.add(sc.nextInt()); 13 } 14 15 int second = sc.nextInt(); 16 for(int i =0; i< second; i++) { 17 set.add(sc.nextInt()); 18 } 19 //foreach循环遍历输出 20 // for(Integer i : set) { 21 // System.out.print(i); 22 // } 23 //迭代器遍历输出 24 Iterator it = set.iterator(); 25 while(it.hasNext()){ 26 System.out.print(it.next()); 27 } 28 System.out.println(); 29 } 30 } 31 }
浙公网安备 33010602011771号