java学习笔记之vector的排序

Vector的基本类型排序

对vector的排序有两种,一种是从小到大排序,一种是从大到小排序,sort默认从小到大排序。

代码来啦:

public class Main{  

    static Scanner cin = new Scanner(System.in);  
    static PrintWriter out = new PrintWriter(System.out);  

    public static void main(String[] args) throws IOException {  
        Vector<Integer> vector = new Vector<Integer>();  
        Random random = new Random();  
        for(int i = 0; i < 12; i++) {  
            vector.add(random.nextInt(10));  
        }  
        System.out.println(vector);  //[2, 7, 6, 4, 2, 5, 8, 3, 7, 3, 3, 7] 
        Collections.sort(vector,Collections.reverseOrder());  
        System.out.println(vector);  //[8, 7, 7, 7, 6, 5, 4, 3, 3, 3, 2, 2]
        Collections.sort(vector);  
        System.out.println(vector);  //[2, 2, 3, 3, 3, 4, 5, 6, 7, 7, 7, 8]
    }  

}   

转载自:https://blog.csdn.net/zsgg_acm/article/details/50268277

猜你喜欢

转载自blog.csdn.net/l903445981/article/details/80154922