矢量在矢量... Java

Yan*_*uan 2 java vector

我在向量中的向量.... 但我不知道他们中有多少(深度是多少).如何更改最后一个矢量的内容?

set*_*rgo 5

你将要使用......等待它......回归!

这是一个链表的例子

public Node<E> go_deep(Node<E> nodeRef) {
  // base case
  if(nodeRef.next == NULL)
    return nodeRef;

  return go_deep(nodeRef.next);
}
Run Code Online (Sandbox Code Playgroud)

然后你可以通过以下方式获得"最后"节点:

public static void main(String[] args) {
  Node<E> lastNode = go_deep(head);
}
Run Code Online (Sandbox Code Playgroud)

头部是你的第一个项目(在这种情况下是矢量).你也可能有不同的方法用于下一个和前一个...

我正在写这篇文章,你必须定义一个节点,如果你真的希望这个工作,它只是基本的想法......

如果Vector(我的示例中的Node)未通过引用传递:

// if you have two-way references
public static void main(String[] args) {
  Node<E> lastNode = go_deep(head); //returns the last Node
  Node<E> prevNode = lastNode.prev; //returns the Node before
  Node<E> newNode = new Node<E>();      

  // update newNode with lastNode's values
  newNode.foo = lastNode.foo;
  newNode.bar = lastNode.bar + 7;

  prevNode.next = newNode; //newNode is inserted into the structure - lastNode dies :(
}
Run Code Online (Sandbox Code Playgroud)

如果你有单向引用,我们修改go_deep以返回节点及其父节点的数组:

public Node<E>[] go_deep(Node<E> nodeRef) {
  // base case
  // THERE ARE EDGE CASES THAT I'M IGNORING BECAUSE I'M NOT PROGRAMMING FOR YOU!
  if(nodeRef.next.next == NULL) {
    Node<E>[] arr = new Node<E>[2];
    arr[0] = nodeRef; // the "prev" node
    arr[1] = nodeRef.next; // the "last" node
    return arr;
  }

  return go_deep(nodeRef.next);
}
Run Code Online (Sandbox Code Playgroud)

然后在主要:

public static void main(String[] args) {
  Node<E>[] nodes = go_deep(head); //returns the array of nodes
  Node<E> lastNode = nodes[1]; // returns the last Node
  Node<E> prevNode = nodes[0]; //returns the Node before
  Node<E> newNode = new Node<E>();      

  // update newNode with lastNode's values
  newNode.foo = lastNode.foo;
  newNode.bar = lastNode.bar + 7;

  prevNode.next = newNode; //newNode is inserted into the structure - lastNode dies :(
}
Run Code Online (Sandbox Code Playgroud)