- 在java.util.collections包下,用于集合元素反转
reverse()方法底层原理
private static final int REVERSE_THRESHOLD = 18;
Reverses the order of the elements in the specified list.
This method runs in linear time.
@param list the list whose elements are to be reversed.
@throws Unsupported Operation Exception if the specified list or its list-iterator does not support the set operation.
条件1: 如果集合的size小于18,或者集合支持随机访问(例如:ArrayLsit、vector),就调用swap方法;
swap方法 底层实现原理如下:
l.set(index,element):返回值是集合中index被替代之前的元素;
l.get(index) :返回集合坐标index的值
条件2:如果不满足条件1;就使用集合迭代器;注意,这个迭代器是支持双向访问
首先是获得集合的头迭代器;
然后根据size大小获得集合的尾迭代器;
接着使用二分法进行集合反转