【问题标题】:Passing “this” in java constructor在 java 构造函数中传递“this”
【发布时间】:2016-03-29 16:54:19
【问题描述】:

我试图理解arrayList和可迭代方法中的迭代器方法我无法理解在“return new MyArrayListIterator(this);”中使用“this”我知道如何使用“this”但我没有看到这种类型的“这个”之前。这是什么?

public class MyArrayList<T> implements Iterable<T> {

private static final int DEFAULT_CAPACITY = 10;

//private instance fields
private T [] m_objs;
private int m_curIndex; 

//private non-static methods
private T[] allocate(int capacity)
{
    T [] objs = (T[])new Object[capacity];

    if (m_curIndex != 0)
        for (int i = 0; i < m_curIndex; ++i)
            objs[i] = m_objs[i];

    return objs;
}

private class MyArrayListIterator<E> implements Iterator<E> {
    private MyArrayList<E> m_mc;
    private int m_curIndex;

    private MyArrayListIterator(MyArrayList<E> mc)
    {
        m_mc = mc;
        m_curIndex = -1;
    }

    public boolean hasNext()
    {
        return ++m_curIndex < m_mc.m_curIndex;      
    }

    public E next() 
    {           
        return m_mc.m_objs[m_curIndex];
    }       
}

//public constructors
public MyArrayList()
{
    m_objs = (T[])new Object[DEFAULT_CAPACITY];
}

public MyArrayList(int capacity)
{
    if (capacity <= 0)
        throw new IllegalArgumentException("Capacity must be non negative");

    m_objs = (T[])new Object[capacity];
}

//getters
public int capacity() { return m_objs.length;}
public int size() { return m_curIndex;} 

//public methods
public boolean add(T elem)
{
    if (m_objs.length <= m_curIndex)
        m_objs = allocate(m_objs.length * 2);

    m_objs[m_curIndex++] = elem;

    return true;        
}

public void clear()
{
    for (int i = 0; i < m_curIndex; ++i)
        m_objs[i] = null;

    m_curIndex = 0;
}

public void ensureCapacity(int minCapacity)
{       
    if (minCapacity <= 0 && minCapacity <= m_curIndex && m_objs.length > m_curIndex)
        m_objs = allocate(m_curIndex);
    else
        m_objs = allocate(minCapacity);     
}

public T get(int index)
{
    if (index < 0 || index >= m_curIndex)
        throw new IndexOutOfBoundsException("index overflow or underflow");     

    return m_objs[index];
}

public boolean isEmpty() { return m_curIndex == 0;}


public Iterator<T> iterator()
{
    return new MyArrayListIterator<T>(this);                
}

public T remove(int index)
{
    if (index < 0 || index >= m_curIndex) 
        throw new IndexOutOfBoundsException("index overflow or underflow");     

    T oldElem = m_objs[index];  

    //TODO:     

    m_curIndex--;

    return oldElem;
}

public void trimToSize()
{
    int capacity = 0;

    if (m_curIndex == 0)
        capacity = DEFAULT_CAPACITY;
    else
        capacity = m_curIndex;

    m_objs = allocate(capacity);
}

public T[] toArray()
{
    return allocate(m_curIndex);
}

}

【问题讨论】:

    标签: java arraylist collections iterator iterable


    【解决方案1】:
    return new MyArrayListIterator<T>(this);     
    

    this 指的是Iterable MyArrayList。你的方法应该在MyArrayList 上返回一个Iterator,所以调用

    return this;
    

    会错的。

    【讨论】:

      【解决方案2】:

      无论何时何地你看到this,它总是指向对象的当前实例。在这种情况下,它指向MyArrayListIterator&lt;E&gt;

      public Iterator<T> iterator()
      {
          return new MyArrayListIterator<T>(this);                
      }
      

      表示MyArrayListIterator的类型是Iterator,使用构造函数this指的是构造函数

      private MyArrayListIterator(MyArrayList<E> mc)
          {
              m_mc = mc;
              m_curIndex = -1;
          }
      

      【讨论】:

      • 我可以说 MyArrayList mc = new MyArrayListIterator
      • @berione 不,你不能。您正在尝试将Iterator分配到Iterable。
      猜你喜欢
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-23
      • 1970-01-01
      相关资源
      最近更新 更多