使用两个集合搞定

简单错误记录

https://www.nowcoder.com/practice/2baa6aba39214d6ea91a2e03dff3fbeb

import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        List<String> list = new ArrayList<>();
        Map<String, Integer> map = new LinkedHashMap<>();
        while (sc.hasNextLine()) {
            String nextLine = sc.nextLine();
            if (nextLine.isEmpty()) {
                break;
            }
            String[] split = nextLine.split(" ");
            String[] pathArr = split[0].split("\\\\");

            String fileName = pathArr[pathArr.length - 1];
            if (fileName.length() > 16) {
                // 注意索引从0开始
                fileName = fileName.substring(fileName.length() - 16);
            }
            HashMap<String, Integer> hashMap = new HashMap<>(1);
            String errorData = fileName + " " + split[1];

            if (!list.contains(errorData)) {
                list.add(errorData);
            }
            if (map.get(errorData) == null) {
                map.put(errorData, 1);
                continue;
            }
            // 之前有存,次数加1
            map.put(errorData, map.get(errorData) + 1);
        }

        if (list.size() <= 8) {
            for (String string : list) {
                System.out.println(string + " " + map.get(string));
            }
        } else {
            for (int i = list.size() - 8; i < list.size(); i++) {
                System.out.println(list.get(i) + " " + map.get(list.get(i)));
            }
        }

    }
}

全部评论

相关推荐

评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务