src/share/classes/java/util/Collections.java
Print this page
@@ -150,10 +150,13 @@
* detects that the natural ordering of the list elements is
* found to violate the {@link Comparable} contract
*/
public static <T extends Comparable<? super T>> void sort(List<T> list) {
Object[] a = list.toArray();
+ if(a.length <= 1) {
+ return;
+ }
Arrays.sort(a);
ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
i.set((T)a[j]);
@@ -212,15 +215,18 @@
* @throws IllegalArgumentException (optional) if the comparator is
* found to violate the {@link Comparator} contract
*/
public static <T> void sort(List<T> list, Comparator<? super T> c) {
Object[] a = list.toArray();
+ if(a.length <= 1) {
+ return;
+ }
Arrays.sort(a, (Comparator)c);
- ListIterator i = list.listIterator();
+ ListIterator<T> i = list.listIterator();
for (int j=0; j<a.length; j++) {
i.next();
- i.set(a[j]);
+ i.set((T) a[j]);
}
}
/**