最近接手外包项目,看代码基本是直接嵌套框架了,两个项目结构写法都差不多,里面还是有能借鉴的地方,今天记录一个。
LinkedList的remove(int index)方法
按照我之前理解,应该是删除对应位置元素,实际不止,先看下注释吧
/**
* Removes the element at the specified position in this list. Shifts any
* subsequent elements to the left (subtracts one from their indices).
* Returns the element that was removed from the list.
*
* @param index the index of the element to be removed
* @return the element previously at the specified position
* @throws IndexOutOfBoundsException {@inheritDoc}
*/
意思大概是这样;在该列表中的指定位置移除元素。转移任何
左边的后续元素(从索引中减去一个元素)。
返回从列表中移除的元素.
也就是说不光会移除,还会返回移除的元素
下面说一下项目中怎么用的
public class TaskPool {
private List<INetModel> taskQueue = new LinkedList<>();
public void addTask(INetModel updateModel) {
taskQueue.add(updateModel);
}
public void execute(final OnNetRequestListener onUpDateListener) {
if (!taskQueue.isEmpty()) {
INetModel updateModel = taskQueue.remove(0);
updateModel.request();
} else
onUpDateListener.onSuccess();
}
}
当一个界面有多个请求,但是要等所有请求都完事才返回成功的时候,可以用到。
看代码;
taskPool.addTask(请求1);
taskPool.addTask(请求2);
taskPool.addTask(请求3);
taskPool.execute(this);
执行execute时,先执行请求1,然后移除请求1,如果列表还有数据,再执行请求2,再移除请求2,当都执行完后返回成功。
总结一下,由于没看到remove的含义,所以研究了一下,这种场景应该挺常见的,当然应该有更好的写法,现在只是根据项目来自己总结一下。