java旧题复习
6-1 Duplicated Words (20分)
This program reads a lot of words, in which may be duplicated words. The program picks out all the duplicated ones and sorts the remainders in a descendent order.
函数接口定义:
public static ArrayList pick(ArrayList a);
a is the ArrayList to be parsed and returns the result as an ArrayList.
裁判测试程序样例:
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.Scanner;
public class Main {
/* 请在这里填写答案 */
public static void main(String[] args) {
ArrayList<String> lst = new ArrayList<>();
Scanner in = new Scanner(System.in);
while ( in.hasNext() ) {
lst.add(in.next());
}
lst = pick(lst);
for ( String x:lst) {
System.out.print(x+" ");
}
System.out.println();
in.close();
}
}
输入样例:
this is a test at this Zhejiang University
输出样例:
this test is at a Zhejiang University
ans:
public static ArrayList<String> pick(ArrayList<String> a){
ArrayList<String> list=new ArrayList<>();
for(int i=0;i<a.size();i++) {
if(list.contains(a.get(i))==false) {
list.add(a.get(i));
}
}
Collections.sort(list);
Collections.reverse(list);
return list;
}