• Post Reply Bookmark Topic Watch Topic
  • New Topic

returning object that has been removed using the remove() method  RSS feed

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i have used the "remove()" method in conjunction with an iterator to remove an object from a HashSet in my program, and part of my exercise requires me to return said object. Can someone point me in the right direction as to what I can do to return an object that has been removed using "remove()" method?

thanks in advance.
 
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So, you were iterating some collection, for example a List, using an Iterator, and then you removed the current object by calling remove() on the iterator?

You probably already have the object, which is what Iterator.next() returns:

What does your code look like?
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:So, you were iterating some collection, for example a List, using an Iterator, and then you removed the current object by calling remove() on the iterator?

You probably already have the object, which is what Iterator.next() returns:

What does your code look like?


Hi,
im having the same problem. Heres my code.



However i need to return the packet that was removed instead of creating a new one.
I tried return (Biscuits)iter.next(); but it doesnt work if there is only one object in the arraylist.
 
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kieran Ireland wrote:However i need to return the packet that was removed instead of creating a new one.
I tried return (Biscuits)iter.next(); but it doesnt work if there is only one object in the arraylist.


Yes, you don't want to return the next one after the one you removed, you want to remove the one you removed. Which as Jesper said, is "pack" in your case. So the line of code you're looking for is



Make sure you put it in the right place.
 
Kenneth Islington
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:


Make sure you put it in the right place.


I thought this however it has to be in the inner if loop or the while, which then causes me an error of a missing return statement.
I really cant get my head around this. I have spent hours trying to figure it hour today.
 
Paul Clapham
Sheriff
Posts: 22846
43
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you're right. That return statement does have to be in the inner loop. But the error message is also right and it's trying to tell you something. Namely, what should the method return if no biscuits were removed from the packet?

Normally the specifications for the method should tell the programmer that, so have a look at your specs. What do they say? (They may be incomplete.)
 
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kieran Ireland wrote:I thought this however it has to be in the inner if loop or the while, which then causes me an error of a missing return statement.
I really cant get my head around this. I have spent hours trying to figure it hour today.

Then sometimes the best thing to do is to StopCoding (←click).

Forget about Java, and describe what you want to happen in English. I suspect you're awfully close; you're just getting yourself tied in knots with Java syntax.

Presumably, this code is part of some larger method - so, what's it trying to do? Maybe you could show it to us; it might help us advise you better.

HIH

Winston
 
Kenneth Islington
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! I think I figured it out in the end. I was missing a return null; for the first if statement. For some reason I thought there shouldn't be another return statement.
 
Marshal
Posts: 56608
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You realise that
return null;
is potentially dangerous?
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!