我在使用Java的remove方法时遇到了问题。

huangapple go评论81阅读模式
英文:

I have a problem with my Java remove method

问题

以下是翻译好的内容:

作为一项任务,我必须编写自己的Java remove方法,该方法使用节点从列表中的指定索引处删除元素。这段代码是我上C#课时写的,当时它没有索引参数,所以我不得不在这里实现它。

所以这段代码实际上移除了指定的元素,但它也移除了顶部元素,这是主要问题。

示例:

 1| ========初始列表=======
 2|   0:  S763   ring       item      1964        1999   258.61 
 3|   1:  P365   textbook   book      1936        2001    64.96 
 4|   2:  G965   forest     photo     1929        2014    30.34 
 5|   3:  K637   diary      journal   1929        1998    98.68 
 6|   4:  L161   girl       photo     1903        1995    54.73 
 7| ======== 列表末尾 =======
 8| ========测试删除索引为3的元素=======
 9|   0:  G965   forest     photo     1929        2014    30.34 
10|   1:  L161   girl       photo     1903        1995    54.73 
11| ======== 列表末尾 =======

正如您所见,它确实删除了索引为3的元素,但也删除了索引为0和1的元素。

以下是Remove代码:

        public E remove(int ind)
        {            
            current = first.findNode(ind);
         
            if (first == null) return current.element;
                        
            if (first == current)
            {
                first = current.next;
                size--;
                return current.element;
            }
           
            while (first.next != null)
            {
                if (first.next == current)
                {
                    first.next = first.next.next;
                    return current.element;
                }

                first = first.next;
            }
            
            size--;
            return current.element;
        }

我想问题出在while循环中,但我不知道如何再次更改它。非常感谢您的帮助。

英文:

As an assignment, I have to write my own Java remove method that removes an element of the specified index from the list using node. This code was taken from the time I had C# class and it didn't have the index parameter so I had to implement it here.

So this code actually removes the specified element but it also removes top elements too which is the main problem.

Exemple:

 1| ========Inital list=======
 2|   0:  S763   ring       item      1964        1999   258.61 
 3|   1:  P365   textbook   book      1936        2001    64.96 
 4|   2:  G965   forest     photo     1929        2014    30.34 
 5|   3:  K637   diary      journal   1929        1998    98.68 
 6|   4:  L161   girl       photo     1903        1995    54.73 
 7| ======== End of the list =======
 8| ========Testing remove at index 3=======
 9|   0:  G965   forest     photo     1929        2014    30.34 
10|   1:  L161   girl       photo     1903        1995    54.73 
11| ======== End of the list =======

As you can see it did remove the element at index 3 but it also removed elements at index 0 and 1.

Here is the Remove code:

        public E remove(int ind)
        {            
            current = first.findNode(ind);
         
            if (first == null) return current.element;
                        
            if (first == current)
            {
                first = current.next;
                size--;
                return current.element;
            }
           
            while (first.next != null)
            {
                if (first.next == current)
                {
                    first.next = first.next.next;
                    return current.element;
                }

                first = first.next;
            }
            
            size--;
            return current.element;
        }

I imagine the problem is in the while loop but I don't know how to change it anymore. Your help will be appreciated a lot.

答案1

得分: 0

在你的代码顶部,你有:

current = first.findNode(ind);
if (first == null) return current.element;

在第二行,如果 first 确实是 null,那么在第一行程序将会崩溃,因为会出现 null 引用异常!你怎么能对一个 null 引用调用 findNode() 呢?

此外,如果 first 是 null,current 会指向什么呢?它也不是 null 吗?我假设 first 指向你的列表中的第一个节点,如果 first 是 null,那么你的列表一定是空的吗?……

我认为你的程序在处理空列表时可能不够优雅。

你的 while 循环的问题在于你正在遍历列表时改变了 first

创建一个临时的局部变量,指向 first,然后在修改这个临时变量,这样你的 first 仍然会指向第一个元素:

Node temp = first; // <-- 我不知道你的节点类的类型
while (temp.next != null)
{
    if (temp.next == current)
    {
        temp.next = current.next;
        break;
    }
    temp = temp.next;
}
size--;
return current.element;

请注意,我没有从循环的中间返回,而是使用了 break; 语句来退出循环。然后在下面,我们会正确地减少列表的大小并返回值。我还将 temp.next.next 改为了 current.next。虽然意思相同,但我觉得这样更有“意义”。

此外,在 ind 值上没有进行错误检查。如果它是负数,或者对于列表来说太大会怎么样呢?

英文:

At the very top of your code, you have:

current = first.findNode(ind);     
if (first == null) return current.element;

In the second line, if first was indeed null, then the program would have crashed with a null reference exception in the first line! How can you call findNode() against a null reference?

Furthermore, if first was null, what would current be pointing to? Wouldn't it be null as well? I'm assuming that first points to the very first node in your list, and if first is null then your list must be EMPTY?...

I believe your program is going to not handle an empty list very gracefully.

The problem with your while loop is that YOU ARE CHANGING first as you walk the list.

Make a temporary, local variable that points to first, and then change that so that your first will still point to the first element:

Node temp = first; // &lt;-- I don&#39;t know the TYPE of your node class here
while (temp.next != null)
{
	if (temp.next == current)
	{
		temp.next = current.next;
		break;
	}
	temp = temp.next;
}
size--;
return current.element;

Note that instead of returning from the middle of the loop, I'm calling break; to drop out of the loop. Down below we'll then correctly decrement the size of the list and return the value. I also changed from temp.next.next to current.next. Same thing, but it makes more "sense" to me.

Also, there is no error checking being done on the ind value. What if it is negative, or is too big for the list?

huangapple
  • 本文由 发表于 2020年10月25日 21:33:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/64524297.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定