Java 是n';t Arraylist FIFO?
我通过了。但我在文档或谷歌中找不到任何关于它被称为FIFO数据结构的地方Java 是n';t Arraylist FIFO?,java,arraylist,Java,Arraylist,我通过了。但我在文档或谷歌中找不到任何关于它被称为FIFO数据结构的地方 除非我不从列表中移除/删除元素,否则我将按照插入元素的相同顺序获取元素。所以不能数组列表数组列表是随机访问。可以在列表中的任意位置插入和删除元素。是的,您可以将其用作FIFO数据结构,但它并不严格执行此行为。如果您想要严格的FIFO,那么请使用队列。不,它不是FIFO,它由数组支持,并且它提供的方法使它的行为类似于数组,您可以从其源代码中找到它们: /** * The array buffer into which th
除非我不从列表中移除/删除元素,否则我将按照插入元素的相同顺序获取元素。所以不能数组列表
数组列表
是随机访问。可以在列表中的任意位置插入和删除元素。是的,您可以将其用作FIFO数据结构,但它并不严格执行此行为。如果您想要严格的FIFO,那么请使用队列
。不,它不是FIFO
,它由数组
支持,并且它提供的方法使它的行为类似于数组
,您可以从其源代码中找到它们:
/**
* 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);
不,对于FIFO,您可能希望使用
链接列表
或队列
实现之一。数组列表
不是使用头/尾概念的数据结构,所有内容都基于索引。是的,迭代顺序是基于插入顺序的,除非进行了显式排序。Arraylist
将保存添加到列表末尾的最后一个元素。所以它保持了插入的顺序。但它是一个随机访问容器,实际上没有先进先出的概念。顺便说一句,仅仅根据ArrayList
内部工作的方式,将其用作FIFO队列不是一个好主意。移除索引0处的元素会导致整个数组移位,即它是一个O(n)操作。如果ArrayList
可以用作某种队列,那么它就是后进先出。(但不要使用ArrayList
作为队列。您可能会使用ArrayDeque
或更专业的东西)明白了。我的观点是,如果我们在最初创建元素时不添加/删除它,那么它的顺序将是FIFO。另外,当我说add/remove时,它意味着不显式地提到索引。@scottmiles如果不添加和删除调用ArrayList“FIFO”的元素,则没有意义。
/**
* 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();