File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Adding null values into a LinkedList

 
Ferghal Smyth
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ferghal Smyth
Greenhorn
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47292
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 120
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20388
46
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic