您的位置:首页 > 其它

AbstractList的迭代器remove()方法疑问

2015-08-09 00:00 357 查看
public void remove() {
if (lastRet == -1)
throw new IllegalStateException();
checkForComodification();

try {
AbstractList.this.remove(lastRet);
if (lastRet < cursor)//这个判断的作用是?
cursor--;
lastRet = -1;
expectedModCount = modCount;
} catch (IndexOutOfBoundsException e) {
throw new ConcurrentModificationException();
}
}


自己写了一个模拟类,测试发现不加这个判断也可以正常删除,不知道这判断的作用是什么。。。。。求高人指点

/**
* 模仿LinkedList
*/
public class SimulateLinkedList {
private Node first;
private Node last;
private int size;
public int size(){
return this.size;
}
/**
* 增加元素
* @param obj
*/
public void add (Object obj){
Node node = null;
if(first==null){
node = new Node(null,obj,null);
first = node;
last = node;
}else{
node = new Node(last,obj,null);
last.next = node;
last = node;
}
size++;
}
/**
* 在指定位置增加元素
* @param index
* @param obj
*/
public void add(int index,Object obj){
Node temp = node(index);
if(temp!=null){
Node node = null;
if(index > 0){  //非头结点
if(index !=size) {//非末尾结点
node = new Node(temp.previous,obj,temp);
temp.previous.next = node;
}else{//末尾结点
node = new Node(temp,obj,null);
last.next = node;
last = node; //更新末尾结点
}
}else{//头结点
node = new Node(null,obj,temp);
first = node;//更新链头结点
}
temp.previous = node;
size++;
}
}
/**
* 获取元素
* @param index
* @throws Exception
*/
public Object get(int index) throws Exception{
rangeCheck(index);
Node temp = node(index);
return temp.element;
}
public void rangeCheck(int index) throws Exception {
if(index<0||index>=size){
throw new Exception(index +" out of " + size);
}
}
/*
* 获取指定位置的节点
*/
private Node node(int index){
Node temp = null;
if(index > (size >> 1)){//二分法查询
temp = last;
for (int i = size -1; i > index; i--) {
temp = temp.previous;
}
}else{
temp = first;
for(int i = 0;i< index;i++){
temp = temp.next;
}
}
return temp;
}
/**
* 删除元素
* @param index
* @return
* @throws Exception
*/
public Object remove(int index) throws Exception{
rangeCheck(index);
Node temp = node(index);
if(temp.previous==null && temp.next == null){
first = last = null;
}else if(temp.previous==null){
temp.next.previous = null;
first = temp.next;
}else if(temp.next==null){
temp.previous.next = null;
last = temp.previous;
}else{
temp.previous.next = temp.next;
temp.next.previous = temp.previous;
}
size--;
return temp;
}

public Iterator iterator(){
return new Iter();
}

@SuppressWarnings({ "unused", "rawtypes" })
private class Iter implements Iterator {
private int cursor;
private int lastRet;

@Override
public boolean hasNext() {
return cursor!=size();
}

@Override
public Object next() {
Object temp = null;
try {
temp = get(cursor);
lastRet = cursor++;
} catch (Exception e) {
e.printStackTrace();
}
return temp;
}

@Override
public void remove() {
if(lastRet ==-1){
throw new IllegalArgumentException();
}
try {
SimulateLinkedList.this.remove(lastRet);
//if(lastRet < cursor){
cursor--;
//}
lastRet = -1;
} catch (Exception e) {
e.printStackTrace();
}
}

}
//节点
class Node{
private Node previous;
private Object element;
private Node next;
public Node(Node previous, Object element, Node next) {
super();
this.previous = previous;
this.element = element;
this.next = next;
}
public Node getPrevious() {
return previous;
}
public void setPrevious(Node previous) {
this.previous = previous;
}
public Object getElement() {
return element;
}
public void setElement(Object element) {
this.element = element;
}
public Node getNext() {
return next;
}
public void setNext(Node next) {
this.next = next;
}
}
}


测试类

public class TestSimulateLinkedList {
private SimulateLinkedList list = null;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}

@AfterClass
public static void tearDownAfterClass() throws Exception {
}

@Before
public void setUp() throws Exception {
list = new SimulateLinkedList();
list.add("aaa");
list.add(111);
list.add(222);
list.add(333);
}

@After
public void tearDown() throws Exception {
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
list = null;
}

@Test
public void testAdd() {
System.out.println(list.size());
}
@Test
public void testAddEx() {
list.add(1,543);
System.out.println(list.size());
System.out.println("========================");
}
@Test
public void testGet() throws Exception {
System.out.println(list.get(1));
}
@Test
public void testRemove() throws Exception {
list.remove(0);
System.out.println(list.size());
}

@Test
public void testIterRemove() throws Exception {
Iterator iter = list.iterator();
while(iter.hasNext()){
System.out.println(iter.next());
iter.remove();
}
System.out.println("================="+list.size());
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: