从AbstractList中创建只读的List

zhangrongchao_ 2014-08-19 02:18:24
package collection;

import java.util.AbstractList;

public class CountingIntegerList extends AbstractList<Integer>{
private int size;

public CountingIntegerList(int size){
this.size = size <0 ? 0 : size;
}
public Integer get(int index) {
return Integer.valueOf(index);
}

public int size() {
return size;
}

public static void main(String[] args) {
System.out.println(new CountingIntegerList(30));
}

}

输出:[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29]

有谁能为我解释一下,为什么会有这样的输出。
...全文
239 9 打赏 收藏 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhangrongchao_ 2014-08-20
  • 打赏
  • 举报
回复
引用 8 楼 shixitong 的回复:
[quote=引用 7 楼 zhangrongchao_ 的回复:] [quote=引用 5 楼 shixitong 的回复:] 1、public abstract class AbstractList<E>类的内部类

private class Itr implements Iterator<E> {
        /**
         * Index of element to be returned by subsequent call to next.
         */
        int cursor = 0;

        /**
         * Index of element returned by most recent call to next or
         * previous.  Reset to -1 if this element is deleted by a call
         * to remove.
         */
        int lastRet = -1;

        /**
         * The modCount value that the iterator believes that the backing
         * List should have.  If this expectation is violated, the iterator
         * has detected concurrent modification.
         */
        int expectedModCount = modCount;

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

        public E next() {
            checkForComodification();
            try {
                int i = cursor;
                E next = get(i);
                lastRet = i;
                cursor = i + 1;
                return next;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }

        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                AbstractList.this.remove(lastRet);
                if (lastRet < cursor)
                    cursor--;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException e) {
                throw new ConcurrentModificationException();
            }
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }
中用到了你实现的size()方法 public boolean hasNext() { return cursor != size(); } 2、toString()方法中又用到了这个迭代器
public String toString() {
        Iterator<E> it = iterator();
        if (! it.hasNext())
            return "[]";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (;;) {
            E e = it.next();
            sb.append(e == this ? "(this Collection)" : e);
            if (! it.hasNext())
                return sb.append(']').toString();
            sb.append(',').append(' ');
        }
    }
你的意思是,我在printfln中调用了toString()方法中调用了iterator(),同时又调用了size(),接着迭代创造出了具有size个对象的List是么?[/quote] 是你的toString方法调用了iterator,然后迭代器用掉了size()方法[/quote] 多谢了,学习了
shixitong 2014-08-20
  • 打赏
  • 举报
回复
引用 7 楼 zhangrongchao_ 的回复:
[quote=引用 5 楼 shixitong 的回复:] 1、public abstract class AbstractList<E>类的内部类

private class Itr implements Iterator<E> {
        /**
         * Index of element to be returned by subsequent call to next.
         */
        int cursor = 0;

        /**
         * Index of element returned by most recent call to next or
         * previous.  Reset to -1 if this element is deleted by a call
         * to remove.
         */
        int lastRet = -1;

        /**
         * The modCount value that the iterator believes that the backing
         * List should have.  If this expectation is violated, the iterator
         * has detected concurrent modification.
         */
        int expectedModCount = modCount;

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

        public E next() {
            checkForComodification();
            try {
                int i = cursor;
                E next = get(i);
                lastRet = i;
                cursor = i + 1;
                return next;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }

        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                AbstractList.this.remove(lastRet);
                if (lastRet < cursor)
                    cursor--;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException e) {
                throw new ConcurrentModificationException();
            }
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }
中用到了你实现的size()方法 public boolean hasNext() { return cursor != size(); } 2、toString()方法中又用到了这个迭代器
public String toString() {
        Iterator<E> it = iterator();
        if (! it.hasNext())
            return "[]";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (;;) {
            E e = it.next();
            sb.append(e == this ? "(this Collection)" : e);
            if (! it.hasNext())
                return sb.append(']').toString();
            sb.append(',').append(' ');
        }
    }
你的意思是,我在printfln中调用了toString()方法中调用了iterator(),同时又调用了size(),接着迭代创造出了具有size个对象的List是么?[/quote] 是你的toString方法调用了iterator,然后迭代器用掉了size()方法
zhangrongchao_ 2014-08-19
  • 打赏
  • 举报
回复
引用 5 楼 shixitong 的回复:
1、public abstract class AbstractList<E>类的内部类

private class Itr implements Iterator<E> {
        /**
         * Index of element to be returned by subsequent call to next.
         */
        int cursor = 0;

        /**
         * Index of element returned by most recent call to next or
         * previous.  Reset to -1 if this element is deleted by a call
         * to remove.
         */
        int lastRet = -1;

        /**
         * The modCount value that the iterator believes that the backing
         * List should have.  If this expectation is violated, the iterator
         * has detected concurrent modification.
         */
        int expectedModCount = modCount;

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

        public E next() {
            checkForComodification();
            try {
                int i = cursor;
                E next = get(i);
                lastRet = i;
                cursor = i + 1;
                return next;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }

        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                AbstractList.this.remove(lastRet);
                if (lastRet < cursor)
                    cursor--;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException e) {
                throw new ConcurrentModificationException();
            }
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }
中用到了你实现的size()方法 public boolean hasNext() { return cursor != size(); } 2、toString()方法中又用到了这个迭代器
public String toString() {
        Iterator<E> it = iterator();
        if (! it.hasNext())
            return "[]";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (;;) {
            E e = it.next();
            sb.append(e == this ? "(this Collection)" : e);
            if (! it.hasNext())
                return sb.append(']').toString();
            sb.append(',').append(' ');
        }
    }
你的意思是,我在printfln中调用了toString()方法中调用了iterator(),同时又调用了size(),接着迭代创造出了具有size个对象的List是么?
skgary 2014-08-19
  • 打赏
  • 举报
回复
如果要创建一个只读的list,那么,你从现成的list里继承,然后把remove之类的方法给override掉。。。
shixitong 2014-08-19
  • 打赏
  • 举报
回复
1、public abstract class AbstractList<E>类的内部类

private class Itr implements Iterator<E> {
        /**
         * Index of element to be returned by subsequent call to next.
         */
        int cursor = 0;

        /**
         * Index of element returned by most recent call to next or
         * previous.  Reset to -1 if this element is deleted by a call
         * to remove.
         */
        int lastRet = -1;

        /**
         * The modCount value that the iterator believes that the backing
         * List should have.  If this expectation is violated, the iterator
         * has detected concurrent modification.
         */
        int expectedModCount = modCount;

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

        public E next() {
            checkForComodification();
            try {
                int i = cursor;
                E next = get(i);
                lastRet = i;
                cursor = i + 1;
                return next;
            } catch (IndexOutOfBoundsException e) {
                checkForComodification();
                throw new NoSuchElementException();
            }
        }

        public void remove() {
            if (lastRet < 0)
                throw new IllegalStateException();
            checkForComodification();

            try {
                AbstractList.this.remove(lastRet);
                if (lastRet < cursor)
                    cursor--;
                lastRet = -1;
                expectedModCount = modCount;
            } catch (IndexOutOfBoundsException e) {
                throw new ConcurrentModificationException();
            }
        }

        final void checkForComodification() {
            if (modCount != expectedModCount)
                throw new ConcurrentModificationException();
        }
    }
中用到了你实现的size()方法 public boolean hasNext() { return cursor != size(); } 2、toString()方法中又用到了这个迭代器
public String toString() {
        Iterator<E> it = iterator();
        if (! it.hasNext())
            return "[]";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (;;) {
            E e = it.next();
            sb.append(e == this ? "(this Collection)" : e);
            if (! it.hasNext())
                return sb.append(']').toString();
            sb.append(',').append(' ');
        }
    }
zhangrongchao_ 2014-08-19
  • 打赏
  • 举报
回复
引用 3 楼 zhangrongchao_ 的回复:
[quote=引用 1 楼 bree06 的回复:] 没有问题呀,输出的下标 public Integer get(int index) { return Integer.valueOf(index); }
为什么会有30个整数输出,size()和size在List的构造中起了什么作用,AbstractList中的迭代器时不时默认参与了循环?[/quote] 就是说为什么传入一个30就构建除了含有30个整数对象的List
zhangrongchao_ 2014-08-19
  • 打赏
  • 举报
回复
引用 1 楼 bree06 的回复:
没有问题呀,输出的下标 public Integer get(int index) { return Integer.valueOf(index); }
为什么会有30个整数输出,size()和size在List的构造中起了什么作用,AbstractList中的迭代器时不时默认参与了循环?
zhjdg 2014-08-19
  • 打赏
  • 举报
回复
看源码
CountingIntegerList a = new CountingIntegerList(30);
		String b = a.toString();				
		System.out.println(b);
public String toString() {
        Iterator<E> it = iterator();
        if (! it.hasNext())
            return "[]";

        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for (;;) {
            E e = it.next();
            sb.append(e == this ? "(this Collection)" : e);
            if (! it.hasNext())
                return sb.append(']').toString();
            sb.append(',').append(' ');
        }
    }
bree06 2014-08-19
  • 打赏
  • 举报
回复
没有问题呀,输出的下标 public Integer get(int index) { return Integer.valueOf(index); }

51,407

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧