【发布时间】:2010-10-21 03:03:36
【问题描述】:
我必须实现一个方法:
E[] toArray(E[] a) // Pass an array, convert to singly linked list, then return the array.
来自java.utilInterface List<E>
正如我所提到的,我必须传递一个数组,将其转换为单链表,对其进行排序,然后返回该数组。
在Node 类中,我可以使用它:
public Node(E v, Node<E> next) {
// pre: v is a value, next is a reference to remainder of list
// post: an element is constructed as the new head of list
data = v;
nextElement = next;
}
public Node(E v) {
// post: constructs a new tail of a list with value v
this(v,null);
}
public Node<E> next() {
// post: returns reference to next value in list
return nextElement;
}
public void setNext(Node<E> next) {
// post: sets reference to new next value
nextElement = next;
}
public E value() {
// post: returns value associated with this element
return data;
}
public void setValue(E value) {
// post: sets value associated with this element
data = value;
}
我是在叫错树还是有人可以在这里帮我解决这个问题?抱歉,如果这是此类问题的错误位置。
【问题讨论】:
-
对列表进行排序的要求从何而来?到目前为止,您在实现
E[] toArray(E[] a)方面做了哪些尝试? -
你为什么要把它变成一个链表,然后又变成一个数组?这只是为了锻炼吗?
标签: java collections linked-list