简体   繁体   English

这段代码具体用于什么? 我怎么能改变它?

[英]What does this code do specifically? And how can I change it?

public void deleteAfter(Node prev){
    prev.setNext(prev.next().next());
}

This method deletes the new node after the given prev node. 此方法删除给定prev节点之后的新节点。 Could someone explain to me what this code does specifically step by step and how I can change it so that it won't error if prev is the last node on the list. 有人可以向我解释这段代码的具体步骤,以及如何更改它,以便如果prev是列表中的最后一个节点,它将不会出错。

@param prev - The node before the place where it will be inserted. @param prev - 插入位置之前的节点。

prev.next().next() gets the node after the next node (from the one given). prev.next().next()获取prev.next().next()节点之后的节点(来自给定的节点)。 That's then passed to the prev.setNext method, making it the next node. 然后将其传递给prev.setNext方法,使其成为下一个节点。 That essentially removes the node in between prev and the next next node. 这基本上删除了prev下一个下一个节点之间的节点。

Basically, it takes prev -> next_1 -> next_2 ... and turns it into prev -> next_2 ... 基本上,它需要prev -> next_1 -> next_2 ...并将其转换为prev -> next_2 ...

If prev is the last node in the list, then next() should return null (I assume). 如果prev是列表中的最后一个节点,那么next()应该返回null (我假设)。 If that's the case, you can do a null check to avoid an error. 如果是这种情况,您可以进行空检查以避免错误。

public void deleteAfter(Node prev){
    if(prev.next() != null) {
        prev.setNext(prev.next().next());
    }
}

The code sets a new next such that if you had a -> b -> c , now you'll have a -> c . 代码设置了一个新的next ,如果你有a -> b -> c ,现在你将有a -> c

To avoid the error, you can check if next returns null, or maybe invoke hasNext if such a method exists on Node. 为了避免错误,您可以检查next是否返回null,或者如果Node上存在这样的方法,则可以调用hasNext

if(prev.next() != null){
  ...
}

It unlinks the link between the node before the node to be deleted and then links the node before the node to be deleted to the node after the node to be deleted. 它取消链接要删除的节点之前的节点之间的链接,然后将要删除的节点之前的节点链接到要删除的节点之后的节点。

在此输入图像描述

You have some Node, somewhere in a list. 你有一些Node,在列表中的某个地方。 I've highlighted it. 我突出了它。

... - node - node - (prev) - node - node - ...

The first call, is to set the "prev"'s next link prev.setNext(...) but what is that next going to be set to? 第一个调用,是设置“prev”的下一个链接prev.setNext(...)但是接下来要设置的是什么? It will be set to "next's next" or (staring with prev) 它将被设置为“下一个下一个”或(与prev一起盯着)

... - node - node - (prev) - node - node - ...

The prev.next() would be prev.next()将是

... - node - node - prev - (node) - node - ...

And the prev.next().next() would be 并且prev.next()。next()将是

... - node - node - prev - node - (node) - ...

So, after the setting operation, your arrangement would look like 因此,在设置操作之后,您的安排将如下所示

... - node - node - prev  node--node - ...
                        \------/

Note that the prev's new "next node" was previously prev's next, next node. 请注意,prev的新“下一个节点”以前是下一个节点的下一个节点。

If you ever ask yourself "What is this code doing????", there is an excellent chance that the code can be written in a more descriptive way. 如果您曾经问​​过自己“这段代码是做什么的?”,那么很有可能以更具描述性的方式编写代码。 For example: 例如:

public void deleteNodeAfter(Node index) {
    // Borrowed from Bill the Lizard's answer
    if (index.next() != null) {
        index.setNext(index.next().next());
    }
}

To answer you directly: This code deletes a node from a linked list. 直接回答:此代码从链接列表中删除节点。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 $ define()在Java Android Studio源代码中做什么并且如何查看和更改此内容 - What does $define() do in java android studio source code do and how can I view and change this 我不明白这段代码背后的逻辑。 有人可以向我解释它的真正作用和方式吗? - i do not get the logic behind this code. Can someone please explain to me what it really does and how? 什么类加载器“新”使用,我如何更改它? - What classloader does “new” use, and how do i change it? 如何根据注释和命名来解释此代码的作用? - How do I decipher what this code does based on the comments and naming? 如何更改Exception的printStacktrace()函数? - How can I change what printStacktrace() does for Exception? 如何为正在编写反应器上下文的 WebFilter 编写单元测试? 具体来说,我要模拟什么以及如何模拟? - How to write unit test for a WebFilter that is writing a Reactor Context? Specifically, what and how do I mock? 我如何具体检查哪些Java字符串被保留,导致内存泄漏? - How do I inspect specifically what Java strings are being kept around, causing memory leak? 我该怎么做才能加快这段代码的速度? - What can I do to speed up this code? 如何在Scala编译器重写原始Scala代码的[Java / Scala?]代码中看到 - How can I see in what [Java/Scala?] code does Scala compiler rewrites original Scala-code 如何更改我的加密代码,使其只加密给定的字符串,而不加密以前加密的内容? - How do I change my encryption code so that it will only encrypt the given string, and not encrypt what was previously encrypted?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM