【问题标题】:How to implement toArray() within a linked list?如何在链表中实现 toArray()?
【发布时间】:2019-03-13 05:36:19
【问题描述】:

我创建了一个排序的链表类,我唯一​​苦苦挣扎的部分是正确实现 toArray() 方法。

    public class SortedLinkedList<T extends Comparable<T>> implements ListInterface<T>
{

  // container class

  private class LinkedNode<T>
  {
    public T data;
    public LinkedNode<T> next;

    public LinkedNode(T data, LinkedNode<T> next)
    {
      this.data = data;
      this.next = next;
    }
  }

  // private variables

  private LinkedNode<T> head, tail;
  private int size;
  private String name;

  // constructor

  public SortedLinkedList(String name)
  {
    head = null;
    tail = null;
    size = 0;
    this.name = name;
  }

  // core functions

  public void Add(T data)
  {
    size++;

    // creation of new node to be added

    LinkedNode<T> newNode = new LinkedNode<T>(data, null);

    // check for empty list; adds node at head if so

    if (head == null)
    {
      head = newNode;
      return;
    } 

    if (head.data.compareTo(data) < 0)
    {
      head = newNode;
      return;
    }

    // insertion in middle

    LinkedNode<T> current = head;
    LinkedNode<T> prev = null;

    while (current != null)
    {
      if (current.data.compareTo(data) > 0)
      {
        prev.next = newNode;
        newNode.next = current;
        return;
      }
      prev = current;
      current = current.next;
      }

    // insertion at end

    prev.next = newNode;

    return;
  }

  public void Remove(T data)
  {
    if (head == null)
    {
      return;
    }

    LinkedNode<T> current = head.next;

    while (current != null && current.next != null)
    {
      if (current.data.compareTo(current.next.data) == 0)
      {
        current.next = current.next.next;
      } else {
      current = current.next;
      }
    }
    size--;
  }

  public int size()
  {
    return size;
  }

  @SuppressWarnings("unchecked")
  public T[] toArray()
  {
    T[] result = (T[])(new Comparable[size()]);

    int counter = 0;

    for ( T item : )
    {
      result[counter++] = item;
    }
    return result;
  }

我遇到的问题是我应该在我的 for/each 行中的“T item:”之后包含什么。我最近在集合类中实现类似的 toArray() 方法没有问题,因为那是“针对集合中的每个项目”,但由于某种原因,我正在空白处为链接列表放置什么。

【问题讨论】:

    标签: java arrays linked-list toarray


    【解决方案1】:

    试试这个。

      @SuppressWarnings("unchecked")
      public T[] toArray()
      {
        T[] result = (T[])(new Comparable[size()]);
    
        int counter = 0;
    
        for ( LinkedNode<T> cursor = head; cursor != null; cursor = cursor.next )
        {
          result[counter++] = cursor.data;
        }
        return result;
      }
    

    【讨论】:

      【解决方案2】:

      其实你的问题的答案是this:

      @SuppressWarnings("unchecked")
      public T[] toArray() {
          T[] result = (T[])(new Comparable[size()]);
      
          int counter = 0;
      
          for (T item : this) {
              result[counter++] = item;
          }
          return result;
      }
      

      这是正确的,但要做到这一点,你必须实现Iterable&lt;T&gt; 接口:

      public class SortedLinkedList<T extends Comparable<T>> implements ListInterface<T>, Iterable<T> {
          @Override
          public Iterator<T> iterator() {
              return null;
          }
      
          // ...
      }
      

      【讨论】:

        【解决方案3】:

        不要将 for 循环与 linked-lists 一起使用。试试下面的方法:

        LinkedNode cursor = head;
        int index = 0;
        while (cursor != null ){
          result[index] = cursor.data;
          cursor = cursor.next;
          index++;
        }
        

        【讨论】:

          猜你喜欢
          • 2014-12-12
          • 2012-09-05
          • 2010-11-02
          • 1970-01-01
          • 1970-01-01
          • 2018-03-06
          • 2019-09-02
          • 2011-09-26
          相关资源
          最近更新 更多