aspose file tools*
The moose likes Beginning Java and the fly likes Adding null values into a LinkedList Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Adding null values into a LinkedList" Watch "Adding null values into a LinkedList" New topic
Author

Adding null values into a LinkedList

Ferghal Smyth
Greenhorn

Joined: Feb 13, 2009
Posts: 9
Hi,

I'm having a problem replacing an element in my list with a null object.
From what I have read this should be possible but when I try to, the object I want to replace is removed but the the null object is put in its place.
I'm getting a ListIterator for the list and manipulating the list through the ListIterator methods.



Just wanted to know if what im trying to do is possible or a suggestion for a work around

Thanks,
Ferg
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
You need to check what the ListIterator is returning. Is it returning a reference to the node or to the value? If the node, then setting that to null will terminate the List.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

Ferghal Smyth wrote:but when I try to, the object I want to replace is removed but the the null object is put in its place.

Isn't that just what replacing means?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ferghal Smyth
Greenhorn

Joined: Feb 13, 2009
Posts: 9
Sorry I meant to say that I want the object to be replaced with a null.
Instead the object is removed and the size of the list is decreased.
No replacement happens.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39828
    
  28
That's what I meant about terminating the List; setting a node to null will signal that this present node is now the "last" node.
Ferghal Smyth
Greenhorn

Joined: Feb 13, 2009
Posts: 9
I can still traverse the list later on, so the list doesn't become terminated.
I presume using the set method wouldn't terminate the list, it would just replace the last object returned by the iterator to the object passed in.
Moojid Hamid
Ranch Hand

Joined: Mar 07, 2009
Posts: 120
Ferghal Smyth wrote:Sorry I meant to say that I want the object to be replaced with a null.
Instead the object is removed and the size of the list is decreased.
No replacement happens.


I don't see why are you seeing a reduction is size of LinkedList. Is it really a java.util.LinkedList we are talking about?

To test out i wrote this code :


and the output is :

5
5
a
null
c
d
e

Exactly what you wanted.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19760
    
  20

What is your List implementation? I've done some testing, and the following code works as expected for both ArrayList and LinkedList:
Ferghal Smyth
Greenhorn

Joined: Feb 13, 2009
Posts: 9
Hi guys,

Thanks for the help. I've found out why the code wasn't giving me the result I wanted.
Turns out if was acting on a locally created copy of the array.

Thanks again,

Ferg
 
Don't get me started about those stupid light bulbs.
 
subject: Adding null values into a LinkedList