是不是Arraylist FIFO?

sco*_*les 1 java arraylist

我浏览了arraylist docs.但是我没有在docs或google中发现任何将其称为FIFO数据结构的地方?

直到和除非,我不从列表中删除/删除元素,我将按照它们插入的相同顺序获取元素.所以不能arraylist

Cod*_*ice 9

ArrayList是随机访问.您可以在列表中的任何位置插入和删除元素.是的,您可以将其用作FIFO数据结构,但它不会严格执行此行为.如果你想要严格的FIFO,那么请Queue改用.


Hon*_*uan 5

不,它不是a FIFO,它由an支持Array,它提供的方法使它像一个行为Array,你可以从它的源代码中找到它们:

/**
 * The array buffer into which the elements of the ArrayList are stored.
 * The capacity of the ArrayList is the length of this array buffer. Any
 * empty ArrayList with elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA
 * will be expanded to DEFAULT_CAPACITY when the first element is added.
 */
// Android-note: Also accessed from java.util.Collections
transient Object[] elementData; // non-private to simplify nested class access

/**
 * Returns the element at the specified position in this list.
 *
 * @param  index index of the element to return
 * @return the element at the specified position in this list
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public E get(int index);

/**
 * Replaces the element at the specified position in this list with
 * the specified element.
 *
 * @param index index of the element to replace
 * @param element element to be stored at the specified position
 * @return the element previously at the specified position
 * @throws IndexOutOfBoundsException {@inheritDoc}
 */
public E set(int index, E element);

/**
 * Appends the specified element to the end of this list.
 *
 * @param e element to be appended to this list
 * @return <tt>true</tt> (as specified by {@link Collection#add})
 */
public boolean add(E e);
Run Code Online (Sandbox Code Playgroud)

所以你操纵数据的方式是无限的,如果你想要a FIFO,考虑使用Queue,这个FIFO特性是由方法提供的(ArrayDeque用作例子):

/**
 * The array in which the elements of the deque are stored.
 * The capacity of the deque is the length of this array, which is
 * always a power of two. The array is never allowed to become
 * full, except transiently within an addX method where it is
 * resized (see doubleCapacity) immediately upon becoming full,
 * thus avoiding head and tail wrapping around to equal each
 * other.  We also guarantee that all array cells not holding
 * deque elements are always null.
 */
transient Object[] elements; // non-private to simplify nested class access

/**
 * Inserts the specified element at the end of this deque.
 *
 * <p>This method is equivalent to {@link #addLast}.
 *
 * @param e the element to add
 * @return {@code true} (as specified by {@link Collection#add})
 * @throws NullPointerException if the specified element is null
 */
public boolean add(E e);

/**
 * Retrieves and removes the head of the queue represented by this deque.
 *
 * This method differs from {@link #poll poll} only in that it throws an
 * exception if this deque is empty.
 *
 * <p>This method is equivalent to {@link #removeFirst}.
 *
 * @return the head of the queue represented by this deque
 * @throws NoSuchElementException {@inheritDoc}
 */
public E remove();

/**
 * Retrieves, but does not remove, the head of the queue represented by
 * this deque.  This method differs from {@link #peek peek} only in
 * that it throws an exception if this deque is empty.
 *
 * <p>This method is equivalent to {@link #getFirst}.
 *
 * @return the head of the queue represented by this deque
 * @throws NoSuchElementException {@inheritDoc}
 */
public E element();
Run Code Online (Sandbox Code Playgroud)