gpt4 book ai didi

java - 如何修改a的字段的字段....如link.next.next.next(.next*n) n次

转载 作者:行者123 更新时间:2023-12-01 22:58:17 27 4
gpt4 key购买 nike

我有一个保存链接的数组。每个链接都有一个名为 next 的公共(public)字段,它可以保存另一个链接,该链接可以保存更多链接等。当我想删除东西时我可以做

    array[x].next = array[x].next.next;

这将删除第二项。但我希望能够删除任何项目,那么如何将其放入带有变量的表单中呢?等式将是这样的:如果我想删除项目 n 我会这样做

    array[x](.next*(n-1)) = array[x](.next*n);

对于 n = 4 将扩展为

    array[x].next.next.next = array[x].next.next.next.next;

希望我的问题很清楚。我需要知道如何这样做,因为我无法在链接类中设置 getter 或任何其他代码,并且由于我是代码的唯一所有者,所以我不会错误地设置我的字段。 Java。

最佳答案

假设我们有array[x].next.next.next.next。编译器接受这个并告诉运行时如何到达我们的第四个元素。它是如何做到这一点的?嗯...

Entry current = array[x];
current = current.next;
current = current.next;
current = current.next;
current = current.next;

如果我们将其转换为 for 循环,我们会得到以下结果:

Entry current = array[x];
for ( int i = 0; i < 4; i++ ) {
current = current.next;
}

事实上,这看起来很像java.util.LinkedList是吗:

/**
* Returns the indexed entry.
*/
private Entry<E> entry(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException("Index: "+index+
", Size: "+size);
Entry<E> e = header;
if (index < (size >> 1)) {
// vvv Looks familiar?
for (int i = 0; i <= index; i++)
e = e.next;
// ^^^
} else {
for (int i = size; i > index; i--)
e = e.previous;
}
return e;
}

关于java - 如何修改a的字段的字段....如link.next.next.next(.next*n) n次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23728087/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com