Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/309.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 使用参数复制单个链表(节点节点)_Java_Singly Linked List - Fatal编程技术网

Java 使用参数复制单个链表(节点节点)

Java 使用参数复制单个链表(节点节点),java,singly-linked-list,Java,Singly Linked List,我面临的问题是如何编写一个方法 public SingleLinkedList copy(Node <E> node) { } 问题是您需要将位置作为int传递。我还删除了节点n,因为您无论如何都不需要它。 我认为这应该行得通 public SingleLinkedList copy() { SingleLinkedList<E> temp = new SingleLinkedList<E>(); int i = 0; for(No

我面临的问题是如何编写一个方法

public SingleLinkedList copy(Node <E> node) {

}

问题是您需要将位置作为int传递。我还删除了节点n,因为您无论如何都不需要它。 我认为这应该行得通

public SingleLinkedList copy() {
    SingleLinkedList<E> temp = new SingleLinkedList<E>();
    int i = 0;
    for(Node<E> ref = head ;ref!= null; ref = ref.next){
        temp.add(i++, ref.data);
    }
    return temp;
}

编辑:我忘了删除参数,您根本不需要它。

n的类型是Node,但是add方法需要int。虽然我很确定编译器也告诉过你这一点。为什么连复制法的参数都没有?教授想让我们更难…我想。这使得我相信更难理解。public static void mainString[]args{SingleLinkedList l=new SingleLinkedList;for int I=0;I<10;I++l.addi;}我如何在主要方面测试这一点?这是已具有列表的主方法。请从copy方法中删除参数,您可以这样调用它:SingleLinkedList b=l.copy;
class SingleLinkedList<E> {

private static class Node<E> {
    private E data;//removed final * private final E data
    private Node<E> next;

    private Node(E item) {
        data = item;
    }
}

private Node<E> head;
private int size;

/* Insert item at index, returns true if add is successful. */
public boolean add(int index, E item) {
    if (index < 0 || index > size) {
        throw new IndexOutOfBoundsException("" + index);
    }

    if (index == 0) { // adding to the front
        Node<E> t = head;
        head = new Node<>(item);
        head.next = t;
    } else { // adding anywhere other than front
        Node<E> left = getNode(index - 1);
        Node<E> node = new Node(item);
        Node<E> right = left.next;
        left.next = node;
        node.next = right;
    }
    size++;
    return true;
}

/* Add item at end of list, returns true if successful. */
public boolean add(E item) {
    return add(size, item);
}

/* Return item at index */
public E get(int index) {
    if (index < 0 || index >= size) {
        throw new IndexOutOfBoundsException("" + index);
    }
    return getNode(index).data;
}

/* Return the number of items */
public int size() {
    return size;
}

/* Returns a string representation of the list */
@Override
public String toString() {
    StringBuilder sb = new StringBuilder();
    sb.append("[ ");
    for (Node<E> n = head; n != null; n = n.next) {
        sb.append(n.data);
        sb.append(" ");
    }
    sb.append("]");
    return sb.toString();
}

/* Return the node at location index */
private Node<E> getNode(int index) {
    Node<E> n = head;
    for (int i = 0; i < index; i++)
        n = n.next;
    return n;
}
public SingleLinkedList copy() {
    SingleLinkedList<E> temp = new SingleLinkedList<E>();
    int i = 0;
    for(Node<E> ref = head ;ref!= null; ref = ref.next){
        temp.add(i++, ref.data);
    }
    return temp;
}