📜  使用 Collections.sort() 方法对向量进行排序的Java程序

📅  最后修改于: 2022-05-13 01:54:39.035000             🧑  作者: Mango

使用 Collections.sort() 方法对向量进行排序的Java程序

Java.util.Collections.sort()方法存在于Java.util.Collections 类中。它用于按升序对指定Collection 列表中存在的元素进行排序。

句法:

参数:向量的实例作为参数

插图: Collection.sort() 方法

Let us suppose that our list contains
{"Geeks For Geeks", "Friends", "Dear", "Is", "Superb"}

After using Collection.sort(), we obtain a sorted list as
{"Dear", "Friends", "Geeks For Geeks", "Is", "Superb"}
  1. 创建一个向量实例。
  2. 在向量中添加元素。
  3. 使用方法在函数之前打印向量以说明函数负责什么。
  4. 使用 Collection.sort() 方法。
  5. 排序后打印向量,可以看到排序是升序的。

示例 1:

Java
// Java Program to Sort Vector
// using Collections.sort() Method
  
// Importing Collection and Vector class
import java.util.Collections;
import java.util.Vector;
  
public class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
        // Create a instance vector
        Vector vec = new Vector();
  
        // Insert the values in vector
        vec.add("a");
        vec.add("d");
        vec.add("e");
        vec.add("b");
        vec.add("c");
  
        // Display the vector
        System.out.println("original vector : " + vec);
  
        // Call sort() method
        Collections.sort(vec);
  
        // Display vector after replacing value
        System.out.println("\nsorted vector : " + vec);
    }
}


Java
// Java Program to Sort Vector
// using Collections.sort() Method
  
// Importing Collection and Vector class
import java.util.Collections;
import java.util.Vector;
  
public class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
        // Create a instance vector
        Vector vec = new Vector();
  
        // Insert the values in vector
        vec.add("4");
        vec.add("2");
        vec.add("7");
        vec.add("3");
        vec.add("2");
  
        // Display the vector
        System.out.println("\noriginal vector : " + vec);
  
        // Call sort() method
        Collections.sort(vec);
  
        // Display vector after replacing value
        System.out.println("\nsorted vector : " + vec);
    }
}


输出
original vector : [a, d, e, b, c]

sorted vector : [a, b, c, d, e]

示例 2:

Java

// Java Program to Sort Vector
// using Collections.sort() Method
  
// Importing Collection and Vector class
import java.util.Collections;
import java.util.Vector;
  
public class GFG {
  
    // Main driver method
    public static void main(String args[])
    {
        // Create a instance vector
        Vector vec = new Vector();
  
        // Insert the values in vector
        vec.add("4");
        vec.add("2");
        vec.add("7");
        vec.add("3");
        vec.add("2");
  
        // Display the vector
        System.out.println("\noriginal vector : " + vec);
  
        // Call sort() method
        Collections.sort(vec);
  
        // Display vector after replacing value
        System.out.println("\nsorted vector : " + vec);
    }
}
输出
original vector : [4, 2, 7, 3, 2]

sorted vector : [2, 2, 3, 4, 7]