记录一个机考题目,筛选字符串0-9a-zA-Z,用hashset对字符串去重,并且保证顺序(数组)

import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;

public class Main {

    public static void main(String[] args) {
        
        Scanner sc = new Scanner(System.in);
        LinkedList<String> correct = new LinkedList<>();
        LinkedList<String> error = new LinkedList<>();
        while(sc.hasNextLine()) {
            
            String line = sc.nextLine();
            line = line.trim();
            if(line.length() >= 1) {
                //有字符串
                //检测有其他字符
                int test = 0;
//                for (int i = 0; i < line.length(); i++) {
//                    char c = line.charAt(i);
//                    if((c>='0'&c<='9')|(c>='a'&c<='z')|(c>='A'&c<='Z')) {
//                        test = test + 1;
//                    }
//                }
                
//                if(test<(line.length())) {
//                    error.add(line);
//                }else {
//                    correct.add(line);
//                }
                
                //另一种方法
                boolean matches = line.matches("[0-9a-zA-Z]+");
                if(matches) {
                    //error.add(line);
                    correct.add(line);
                }else {
                    error.add(line);
                    //correct.add(line);
                }
                test = 0;
                
            }else {
                //没有字符串就输出数据
                String result = "";
                //正确数据
                boolean flag = true;
                LinkedList<String> list = new LinkedList<String>(new HashSet<String>(correct));
                //存贮位置
                int[] count = new int[list.size()];
                //查找位置并存储
                for (int i = 0; i < list.size(); i++) {
                    flag = true;
                    for (int j = 0;flag && j < correct.size(); j++) {
                        if(list.get(i).equals(correct.get(j))) {
                            count[i] = j;
                            flag = false;
                        }
                    }
                }
                //位置排序,从correct中取数据
                Arrays.sort(count);
                System.out.println("删减之前:"+correct.toString());
                for (int i = 0; i < count.length; i++) {
                    result = result + correct.get(count[i]) +" ";
                }
                int a = 0;
                for (int i = 0; i < count.length; i++) {
                    correct.remove(count[i]-a);
                    a++;
                }
                System.out.println("重复数据:"+correct.toString());
                result = result.trim();
                System.out.println(result);
                result = "";
                //错误数据输出
                for (int i = 0; i < error.size(); i++) {
                    result = result + error.get(i) +" ";
                }
                result = result.trim();
                System.out.println(result);
            }
            
        }
    }

}

 去重可以用hashset集合,简单方便,但是会造成无序状态

用了一个数组存储数据,数组大小为集合大小

然后根据hashset值找对应的位置序号,并存入数组,值保存第一个,因此,此处设置一个flag来结束内层循环,continue在java中貌似不适用

用arrays进行数组自动排序。自动排序之后的结果即为hashset集合中正确顺序

posted @ 2020-05-11 13:17  小城熊儿  阅读(299)  评论(0编辑  收藏  举报