Modifies the specified List by reversing the order of the elements. @param list the list to reverse. @throws UnsupportedOperationException when replacing an element in the List is not supported.
(List<?> list)
| 1785 | * when replacing an element in the List is not supported. |
| 1786 | */ |
| 1787 | @SuppressWarnings("unchecked") |
| 1788 | public static void reverse(List<?> list) { |
| 1789 | int size = list.size(); |
| 1790 | ListIterator<Object> front = (ListIterator<Object>) list.listIterator(); |
| 1791 | ListIterator<Object> back = (ListIterator<Object>) list |
| 1792 | .listIterator(size); |
| 1793 | for (int i = 0; i < size / 2; i++) { |
| 1794 | Object frontNext = front.next(); |
| 1795 | Object backPrev = back.previous(); |
| 1796 | front.set(backPrev); |
| 1797 | back.set(frontNext); |
| 1798 | } |
| 1799 | } |
| 1800 | |
| 1801 | /** |
| 1802 | * A comparator which reverses the natural order of the elements. The |