ArrayList.ListIterator(INT指数)与ArrayList.get(INT指数) [英] ArrayList.ListIterator(int index) vs ArrayList.get(int index)

查看:150
本文介绍了ArrayList.ListIterator(INT指数)与ArrayList.get(INT指数)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道用ArrayList.ListIterator时对性能的影响会是什么?(INT指数 - 1),然后对比使用ArrayList.get(INT指数)it.next()

I was wondering what the performance impact would be when using ArrayList.ListIterator(int index - 1), then it.next() in contrast to using ArrayList.get(int index)?

推荐答案

为什么看实现...

public ListIterator<E> listIterator(final int index) {
if (index<0 || index>size())
  throw new IndexOutOfBoundsException("Index: "+index);

return new ListItr(index);
}

private class ListItr extends Itr implements ListIterator<E> {
ListItr(int index) {
    cursor = index;
}

// [...]

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

2:List.get(INT)

public E get(int index) {
RangeCheck(index);

return (E) elementData[index];
}

应该是相当明显的,哪一个是速度更快。有关性能影响的细节,我将与麦克同意。简介吧。不管是什么原因,你想使用这种独特的访问方法只访问一个项目(?)

Should be quite obvious, which one is faster. For details about performance impacts, I will have to agree with Mike. Profile it. Whatever the reason is, you'd like to use such a distinct access method just to access one item (?)

这篇关于ArrayList.ListIterator(INT指数)与ArrayList.get(INT指数)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆