6-4 根据要求,使用泛型和LinkedList编写StringList类,实现QQ号码查找的功能。 (30 分)java版

class StringList {

    public LinkedList<String> constructList(String[] strs) {
        LinkedList<String> ll = new LinkedList<String>();
        HashSet<Integer> hs = new HashSet<Integer>();
        for (int i = 0; i < strs.length; i++) {
            if (hs.contains(strs[i].length())) {
                continue;
            } else {
                ll.add(strs[i]);
                hs.add(strs[i].length());
            }
        }
        return ll;
    }

    public String search(LinkedList<String> list) {
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        String result = "not exist";
        for (String x : list) {
            if (x.length() == n) {
                return x;
            }
        }
        return result;
    }
}

版权声明:本文为weixin_52445323原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。