将LinkedQueue类型的ArrayList的每个元素实例化为字母表中的字母

时间:2017-10-05 00:08:50

标签: java arraylist linked-list

我想实例化LinkedQueue类型的ArrayList的每个元素(大小为26,字母表的每个字母都有一个队列)。在我的头脑中 - 因为我还没有与LinkedQueues合作过,我写道:

public void initializeList() {

    ArrayList<LinkedQueue> arrayList = new ArrayList<LinkedQueue>();
    String letters = "abcdefghijklmnopqrstuvwxyz";
    for(int i = 0; i < letters.length(); i++){
           arrayList.add(letters.charAt(i));
       }
}

但是我得到一个错误:&#34;类型ArrayList中的方法add(LinkedQueue)不适用于参数(char)。&#34;我还没有完全掌握Java;有人可以帮助我理解这意味着什么以及我做错了什么(或者可能推荐更好的解决方案)?

作为参考,这是LinkedQueue类:

public class LinkedQueue<T> implements QueueADT<T>
{
private int count;
private LinearNode<T> head, tail;

/**
 * Creates an empty queue.
 */
public LinkedQueue()
{
    count = 0;
    head = tail = null;
}

/**
 * Adds the specified element to the tail of this queue.
 * @param element the element to be added to the tail of the queue
 */
public void enqueue(T element)
{
    LinearNode<T> node = new LinearNode<T>(element);

    if (isEmpty())
        head = node;
    else
        tail.setNext(node);

    tail = node;
    count++;
}

/**
 * Removes the element at the head of this queue and returns a
 * reference to it. 
 * @return the element at the head of this queue
 * @throws EmptyCollectionException if the queue is empty
 */
public T dequeue() throws EmptyCollectionException
{
    if (isEmpty())
        throw new EmptyCollectionException("queue");

    T result = head.getElement();
    head = head.getNext();
    count--;

    if (isEmpty())
        tail = null;

    return result;
}

/**
 * Returns a reference to the element at the head of this queue.
 * The element is not removed from the queue.  
 * @return a reference to the first element in this queue
 * @throws EmptyCollectionsException if the queue is empty
 */
public T first() throws EmptyCollectionException
{
    if (isEmpty())
            throw new EmptyCollectionException("Queue is empty");
    return head.getElement();

}

/**
 * Returns true if this queue is empty and false otherwise. 
 * @return true if this queue is empty 
 */
public boolean isEmpty()
{
    return count==0;
}

/**
 * Returns the number of elements currently in this queue.
 * @return the number of elements in the queue
 */
public int size()
{
    return count;
}

/**
 * Returns a string representation of this queue. 
 * @return the string representation of the queue
 */
public String toString()
{
    StringBuilder s = new StringBuilder("");
    LinearNode<T> current = head;
    while (current != null) {
            s.append(current.getElement());
            s.append("\n");
            current = current.getNext();
    }
    return s.toString();

}

/**
 * Returns true if the list in the parameter has the same length and data elements
 * as "this" Linked Queue.  Returns false otherwise.
 * @return true if the lists are equal
 */
@Override
public boolean equals(LinkedQueue<T> list) {
    // Lab exercise 1.  Fill in this method.
    return true;
}

/**
 * Returns a LinkedQueue that contains the same data elements as "this", but in 
 * reverse order
 * @return a LinkedQueue that is the reverse of this queue
 */
public LinkedQueue<T> reverse() {
    // Lab exercise 2.  Fill in this method.
    return null;
}

}

0 个答案:

没有答案