~ Mansukh
~ Mansukh
Mansukhdeep Thind wrote:Implemented the ensureCapacity(int) method:
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mansukhdeep Thind wrote:I tried my hand at implementing the addAll(Collection <? extends T>) method:
It is functioning properly.
One doubt though. The API documentation says that it throws NullPointerException if collection is null. Am I supposed to cater for that in my method too? I ask this because NullPointerException is a Run time type, unchecked exception.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Mansukhdeep Thind wrote:Implemented the ensureCapacity(int) method:
Which doesn't do what you document it to (which is not necessarily wrong; just inconsistent). What if minimumCapacity is less than values.length?
Winston
~ Mansukh
Winston Gutkowski wrote:
Mansukhdeep Thind wrote:I tried my hand at implementing the addAll(Collection <? extends T>) method:
Again, you're waaay overthinking this ... and you've still got it wrong.
1. There's absolutely no need to convert the collection to an array.
2. You already have a perfectly good method for adding elements, so why not use it?
~ Mansukh
Mansukhdeep Thind wrote:The documentation clearly says "Increases.....".
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote:
Mansukhdeep Thind wrote:The documentation clearly says "Increases.....".
But you haven't enforced it. Right now it's just a "resize" method - which you've already written.
Winston
~ Mansukh
Mansukhdeep Thind wrote:
Mansukhdeep Thind wrote:
values = resizeArray(values, minimumCapacity);
Jeff Verdegan wrote:Why are you passing values as an argument to that method? Do you sometimes pass a different array?
"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:Why are you passing values as an argument to that method? Do you sometimes pass a different array?
That was my suggestion. Basically, it allows the method to be used for both resizing and initialization.
Winston
Mansukhdeep Thind wrote:Right, so does this look better:
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:
Is that how you always use an Iterator? That's kind of messed up.
1. You should be using a foreach loop, rather than an explicit Iterator.
2. Even if you do use an explicit iterator, you shouldn't be counting up to size().
3. Even if you're counting up to size(), there's no need to call size() on every iteration.
~ Mansukh
Jeff Verdegan wrote:You mean the client can pass in an array to use? That doesn't sound good. Or am I misunderstanding what you mean by initialization?
"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:You mean the client can pass in an array to use? That doesn't sound good. Or am I misunderstanding what you mean by initialization?
You have to go back about 30 posts.![]()
Jeff Verdegan wrote:
Winston Gutkowski wrote:
Jeff Verdegan wrote:You mean the client can pass in an array to use? That doesn't sound good. Or am I misunderstanding what you mean by initialization?
You have to go back about 30 posts.![]()
No thanks. I'll just trust you on this one.![]()
~ Mansukh
~ Mansukh
~ Mansukh
Mansukhdeep Thind wrote:Are you asking why did I put indexPos++ inside the loop?
EDIT: Why can't I see your post Jeff? I just saw it about a minute ago.
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:Are you asking why did I put indexPos++ inside the loop?
EDIT: Why can't I see your post Jeff? I just saw it about a minute ago.
I removed my post because I didn't pay enough attention to what you were doing, so my comment was irrelevant.
~ Mansukh
Jeff Verdegan wrote:Here's one place where I wouldn't take the simplest approach. If the collection you're adding has M elements, and there are N elements in your existing list at or after indexPos, you'll be doing M * N moves, just moving the same N elements M times. You could just move them once, changing that part of this method from O(M*N) to simply O(N).
Mansukhdeep Thind wrote:
Jeff Verdegan wrote:Here's one place where I wouldn't take the simplest approach. If the collection you're adding has M elements, and there are N elements in your existing list at or after indexPos, you'll be doing M * N moves, just moving the same N elements M times. You could just move them once, changing that part of this method from O(M*N) to simply O(N).
Not sure what you are suggesting here? Do you want me to shift all the elements to the right of the indexPos at once by number of positions equal to size of the collection? And then insert the collection.
~ Mansukh
Mansukhdeep Thind wrote:Is this close to what you wanted Jeff:
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
~ Mansukh
Winston Gutkowski wrote:Actually, there's still a possible inefficiency in what I wrote (yet another reason, if any were needed, not to offer solutions
); I'll leave you to work out why - and it's not obvious.
Winston
~ Mansukh
Mansukhdeep Thind wrote:Well, for that I would first need to understand why you wrote what you just did. I think I would rather pass this one and move on to the next method.
![]()
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Mansukhdeep Thind wrote:I think I would rather pass this one and move on to the next method.
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
~ Mansukh
Winston Gutkowski wrote:
Mansukhdeep Thind wrote:I think I would rather pass this one and move on to the next method.
Fine. But don't forget it.
Winston
~ Mansukh
Mansukhdeep Thind wrote:I got really close to what you just wrote. Didn't I Winston?
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Winston Gutkowski wrote: And if you're worried about making your method bomb-proof from updates to collection, just clone it first, viz:
collection = new ArrayList<T>(collection);
Jeff Verdegan wrote:That doesn't really protect against collection changing. It just narrows the window a little...
"Leadership is nature's way of removing morons from the productive flow" - Dogbert
Articles by Winston can be found here
Jeff Verdegan wrote:
Winston Gutkowski wrote: And if you're worried about making your method bomb-proof from updates to collection, just clone it first, viz:
collection = new ArrayList<T>(collection);
That doesn't really protect against collection changing. It just narrows the window a little. The whole class has no thread-safety in it, so I don't see any point in worrying about it here either. In fact, it's impossible to guarantee that collection won't be structurally modified while during addAll() from within this class. We need cooperation from the caller for that.
~ Mansukh
Don't mess with me you fool! I'm cooking with gas! Here, read this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
|