由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - In java, how do you delete a node in a linked list?
相关主题
[合集] 真心请教:面试的时候被问这种问题变态么? (转载)弱问一下
请教一个javascript的heap的问题。 (转载)C++ Q05: pointer to constant variable
现在cpp真的没用武之地了。高频交易都可以用java做了an important update in clrs 3rd ed
one more c++ question【C++算法求助】有个O(n*n)的算法不知道该怎么优化并且并行化计算
算法题一个linked list vs Binary tree
Reversing a singly linked list问个c++删除链表(linked list)节点的问题
how to assign new value to loop variables?容器里边放指针怎么办?
question about mock请教一个MS Linked List的问题
相关话题的讨论汇总
话题: node话题: obsolete话题: delete话题: java话题: linked
进入Programming版参与讨论
1 (共1页)
s*i
发帖数: 31
1
In c or c++, if given a node in a linked list, and you want to delete the
node after it. You can just:
obsolete_node = node->next
node->next=node->next->next
free(obsolete-node) //or "delete obsolete_node"
But java has garbage collection, and no such keyword as free or delete. So
in java, can i simply skip the last statement, and it'll be correct? Because
obsolete_node no longer has any references to it, so the garbage collector
will free up its memory ?
g*****g
发帖数: 34805
2
You are right.

Because
collector

【在 s*i 的大作中提到】
: In c or c++, if given a node in a linked list, and you want to delete the
: node after it. You can just:
: obsolete_node = node->next
: node->next=node->next->next
: free(obsolete-node) //or "delete obsolete_node"
: But java has garbage collection, and no such keyword as free or delete. So
: in java, can i simply skip the last statement, and it'll be correct? Because
: obsolete_node no longer has any references to it, so the garbage collector
: will free up its memory ?

B********e
发帖数: 1062
3
try obsolete_node = null, which will help the gc to destroy it.

Because
collector

【在 s*i 的大作中提到】
: In c or c++, if given a node in a linked list, and you want to delete the
: node after it. You can just:
: obsolete_node = node->next
: node->next=node->next->next
: free(obsolete-node) //or "delete obsolete_node"
: But java has garbage collection, and no such keyword as free or delete. So
: in java, can i simply skip the last statement, and it'll be correct? Because
: obsolete_node no longer has any references to it, so the garbage collector
: will free up its memory ?

g*****g
发帖数: 34805
4
That's not neccesary since obsolete_node is a local variable.
Actually the assignment for obsolete_node is not neccesary at all.

【在 B********e 的大作中提到】
: try obsolete_node = null, which will help the gc to destroy it.
:
: Because
: collector

B********e
发帖数: 1062
5
believe or not, it's a good habit to assign unused variables to null in java.
local variables could be exceptions though.

【在 g*****g 的大作中提到】
: That's not neccesary since obsolete_node is a local variable.
: Actually the assignment for obsolete_node is not neccesary at all.

g*****g
发帖数: 34805
6
It's a good habit to use as least instance variables as needed in
any OO languages. Instance variables are often abused
just to pass local variables inside the class.
I don't think assigning unused variables to null is neccesary,
unless you are doing it for heavy weight resource, such as an image.
Java GC is smart enough to get the job done.

java.

【在 B********e 的大作中提到】
: believe or not, it's a good habit to assign unused variables to null in java.
: local variables could be exceptions though.

B********e
发帖数: 1062
7
You are smart enough to argue with me (:

【在 g*****g 的大作中提到】
: It's a good habit to use as least instance variables as needed in
: any OO languages. Instance variables are often abused
: just to pass local variables inside the class.
: I don't think assigning unused variables to null is neccesary,
: unless you are doing it for heavy weight resource, such as an image.
: Java GC is smart enough to get the job done.
:
: java.

1 (共1页)
进入Programming版参与讨论
相关主题
请教一个MS Linked List的问题算法题一个
[合集] how to remove duplicates from linked list?Reversing a singly linked list
Re: 请教一道题目how to assign new value to loop variables?
请问这道题怎么解决?question about mock
[合集] 真心请教:面试的时候被问这种问题变态么? (转载)弱问一下
请教一个javascript的heap的问题。 (转载)C++ Q05: pointer to constant variable
现在cpp真的没用武之地了。高频交易都可以用java做了an important update in clrs 3rd ed
one more c++ question【C++算法求助】有个O(n*n)的算法不知道该怎么优化并且并行化计算
相关话题的讨论汇总
话题: node话题: obsolete话题: delete话题: java话题: linked