Mansukhdeep Thind wrote:
Is there NO WAY AT ALL to make this operation atomic Jeff? Can't we somehow achieve a handle on the collection and lock it down when it is passed as an argument to the addAll() method
Why is it impossible?
Mansukhdeep Thind wrote:Is there NO WAY AT ALL to make this operation atomic Jeff?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:You can put a lock on the add method (etc) which prevents two threads accessing the add method simultaneously. You can make the lock re-entrant and lock all the add methods, remove, etc, so only one of them can be called at any one time. But the Collection you are loading the elements from is probably not thread‑safe, so there is the possibility that it is added to or removed from while your addAll operation is proceeding.
~ Mansukh
~ Mansukh
Mansukhdeep Thind wrote:So you want me to implement Iterable interface for my CustomArrayList class?
Campbell Ritchie wrote:You need to work out how to create the Iterator and a ListIterator...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:Why couldn't they simply have extended Iterator to provide a direction? I have never, in 12 years, needed to do next() followed by previous(); which is, arguably, not "iteration" at all.
Winston
extended Iterator to provide a direction..
~ Mansukh
~ Mansukh
Mansukhdeep Thind wrote:
public final boolean hasNext() {
if(iterable[currentIndex]!=null){
return true;
}
return false;
}
~ Mansukh
Mansukhdeep Thind wrote:Then how come the for-each and the while(hasNext()) are returning perfect results. All the elements are being printed correctly in order. If my hasNext() method is flawed as your say, which it is , then why is there no problem with the printing of the list?
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:Then how come the for-each and the while(hasNext()) are returning perfect results. All the elements are being printed correctly in order. If my hasNext() method is flawed as your say, which it is , then why is there no problem with the printing of the list?
Don't get defensive. Just answer my question: Is the only way that you ever don't have a next element is if you're currently pointing to null?
~ Mansukh
Mansukhdeep Thind wrote:
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:Then how come the for-each and the while(hasNext()) are returning perfect results. All the elements are being printed correctly in order. If my hasNext() method is flawed as your say, which it is , then why is there no problem with the printing of the list?
Don't get defensive. Just answer my question: Is the only way that you ever don't have a next element is if you're currently pointing to null?
No Jeff. The correct logic would be to check successively if the next element is null or not. Am I thinking correct now?
Jeff Verdegan wrote: Here's another hint: Will there always be at least one null element?
~ Mansukh
Mansukhdeep Thind wrote:
Jeff Verdegan wrote: Here's another hint: Will there always be at least one null element?
No Jeff. If the list size becomes equal to its capacity, then we will have no null elements at all. But what's the point? I just modified the methods as follows:
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:
Jeff Verdegan wrote: Here's another hint: Will there always be at least one null element?
No Jeff. If the list size becomes equal to its capacity, then we will have no null elements at all. But what's the point? I just modified the methods as follows:
The point is exactly the modification you just made. That's what I was getting it. Your earlier approach that only looked for the next (or current) element being null would throw ArrayIndexOutOfBoundsException if your array was full. I was asking because I didn't know if maybe you always re-sized before you get full, so that there would in fact always be at least one null at the end. (Although i think it would still be good practice to check for cur matching size, so that your hasNext() is less tightly coupled to your backing store implementation.)
Jeff Verdegan wrote:There's another, smaller problem though. Your next() claims to throw NoSuchElementException, but it doesn't. It throws ArrayIndexOutOfBoundsException.
Your testing should include testing error conditions, to make sure that exceptions are thrown when expected, and that they're the correct exceptions.
~ Mansukh
Mansukhdeep Thind wrote:I did not understand this statement of yours Winston. What do you mean when you say
extended Iterator to provide a direction..
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mansukhdeep Thind wrote:Since the backing array is growing as and when size becomes equal to capacity, there will always be at least 1 null element.
This is yet another thing on my to do list Jeff. I need to learn how to judge which exceptions will be thrown and when to throw them. Is there a tutorial for this? Or is it sheer experience of working with the language that helps one gauge it?
Winston Gutkowski wrote:you can can even implement one that allows you to change direction, but it only works in one direction at a time.
Jeff Verdegan wrote:If I can change direction, how is that any more "one direction at a time" then just calling next(); previous(); next(); previous();? Are you talking about only being allowed to change direction after iterating the entire list?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Jeff Verdegan wrote:If I can change direction, how is that any more "one direction at a time" then just calling next(); previous(); next(); previous();? Are you talking about only being allowed to change direction after iterating the entire list?
No, I mean that if you need an Iterator to work in either direction, give it one; don't proliferate methods. Why not just have next()/hasNext() work on the "next" element in whatever direction the Iterator is defined to travel?
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:Since the backing array is growing as and when size becomes equal to capacity, there will always be at least 1 null element.
So, if capacity is 10 (array.length is 10), then when you add the 10th element, so that now all the array slots are filled up, you resize the array before you return from add()? If so, then, yes, I was mistaken and you don't have to check for size == capacity. However, if you return from add() without resizing, then you will not have a null element.
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:This is yet another thing on my to do list Jeff. I need to learn how to judge which exceptions will be thrown and when to throw them. Is there a tutorial for this? Or is it sheer experience of working with the language that helps one gauge it?
You should throw an exception when something outside the expected "happy path" execution occurs. If something that's required to happen fails to happen, throw an exception (unless you can make things right in the code). The exception name, package, and class hierarchy should match the problem.
In the case of java.util.Collection, calling Iterator.next() when there are no more elements throws NoSuchElementException. That is an exception that was created specifically to mean "there are no more elements here, and you should know that, but you tried to get the next one anyway." It was actually created for Enumeration, but that's just an early form of Iterator. The "and you should know that" part is the reason its an unchecked exception.
In your case, ArrayIndexOutOfBoundsException might not necessarily be wrong, if your ArrayList stands alone, rather than being part of a collection library. In the case of java.util.Collection, however, ArrayIndexOutOfBoundsException doesn't make sense, because not all Collections are backed by arrays. So a general-purpose "this Collection has no more elments" exception was defined, to be thrown when you try to go past the end of any Collection. Like so many other things you've been hit with lately, it's about decoupling interface from implementation.
~ Mansukh
Jeff Verdegan wrote:it would be something like?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Campbell Ritchie wrote:That might be better design than ListIterator, but the List interface has a listIterator() method which returns a ListIterator, so you are stuck with hasPrevious() and previous().
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Jeff Verdegan wrote:That's one way. But you're forgetting to use your own methods again.
What's the logic of next()? "If the list has a next element, return it, otherwise, throw an exception."
~ Mansukh
Mansukhdeep Thind wrote:Now, after reading all this fuss created about the ListIterator and Winston's terming it as a dog's breakfast, I am having second second thoughts whether I should go ahead and implement it at all?
![]()
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
What is the meaning of optional? Can I get away without implementing it?Removes from the underlying collection the last element returned by the iterator (optional operation).
~ Mansukh
Mansukhdeep Thind wrote:What is the meaning of optional? Can I get away without implementing it?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
You've got it. My DirectionalIterator simply adds a nextIndex() method (already defined in ListIterator) to the Iterator spec, and I also have a TwoWayIterator (not wild about the name though) that further adds a Direction enum and get/setDirection() methods.
The idea is that the first one is for iterators that have their direction set at construction time, and the second is a building block for a ListIterator as well as being an Iterator that can simply "change direction". I've considered writing a JSR for it, but I'm not sure of the procedure.
Winston Gutkowski wrote:
Mansukhdeep Thind wrote:What is the meaning of optional? Can I get away without implementing it?
Sure. The usual way is to simply have it throw UnsupportedOperationException.
Winston
~ Mansukh
Campbell Ritchie wrote:As Winston says, you have no choice.
~ Mansukh
Mansukhdeep Thind wrote:
Campbell Ritchie wrote:As Winston says, you have no choice.
I just went through the documentation of the ListIterator interface. I don't see any specific thing that I can't do with what I have currently as my public APIs. Why do I need to implement this interface if it does not serve any particular purpose at all? Can you give an example of some use case which can not be tackled by existing APIs and the Iterator functionality already implemented, except for traversal in reverse direction, which I think is more or less useless? Isn't it Campbell?
Jeff Verdegan wrote:Yup.
You might choose to put a bit more detail in the message, such as which class and method.
Some may say that's redundant, since the stack trace contains all that. Others may say it's useful to have it self-contained in the exception's message. It's a judgment call, and pretty much comes down to personal preference. I'm just throwing it out there as something for you to keep in mind.
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime. |