Thoughts on Written Exam Questions of Autumn Recruitment

An ArrayList, the number range is [0,1024], and the odd numbers are output in descending order

 	public static ArrayList<Integer> sortMethodByTreeSet(List<Integer> list){
    
    

        TreeSet<Integer> set = new TreeSet<>(new Comparator<Integer>(){
    
    

            @Override
            public int compare(Integer integer, Integer t1) {
    
    
                return t1.compareTo(integer);
                };
            });
        for (int o : list) {
    
    
            if(o % 2 !=0){
    
    
                set.add(o);
            }
        }
        return new ArrayList<>(set);
    }

This is not the optimal solution. Can the number range from 0 to 1024 be represented by a two-dimensional array...? There is a better solution, you can comment below

Guess you like

Origin blog.csdn.net/weixin_43957211/article/details/110528676