java笔试题常用类

285 阅读1分钟

一 输入

1.1 读取整数、小数、字符串等

    sc.hasNextInt();
    sc.nextInt();
    
    sc.hasNextDouble();
    sc.nextDouble();
    
    sc.next();//获得字符串

1.2 读取一整行

读取到下一个换行符之间的所有数据 要注意在nextInt完可能需要调用两次nextLine才能读到下一行

二 排序

Arrays.sort()用于对数组排序
Collections.sort()用于对List等排序
在最新版本jdk中,用的是mergesort和timsort,所以是稳定的

Collections.sort(list,new Comparator<Person>(){
    @override 
    public int compare(Person p1,Person p2){
        ...
    }
}

Return -1 表示 p1 排在前面
Return 1 表示p2 排在前面
Return 0 表示 相等

三 List

 Queue<Integer> queue = new LinkedList<>();
 queue.offer();
 queue.poll();
 queue.isEmpty();
 queue.peek();
 
 Stack<Integer> stack = new Stack<>();
 stack.push();
 stack.pop();
 stack.peek();
 stack.empty();
 
 PriorityQueue

Map map.keySet() map.values()