在Java中,可以通過Collections.sort()方法對LinkedList中的元素進行排序操作。例如:
import java.util.LinkedList;
import java.util.Collections;
public class Main {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<>();
list.add(5);
list.add(3);
list.add(8);
list.add(1);
System.out.println("Before sorting: " + list);
Collections.sort(list);
System.out.println("After sorting: " + list);
}
}
輸出結果為:
Before sorting: [5, 3, 8, 1]
After sorting: [1, 3, 5, 8]