But think how much he will learn if he ever finishes this class.Winston Gutkowski wrote: . . . Blimey. The thread that refuses to die. . . .
Blimey. Page 3.Winston Gutkowski wrote: . . . Blimey. The thread that refuses to die. . . .
Campbell Ritchie wrote:
But think how much he will learn if he ever finishes this class.Winston Gutkowski wrote: . . . Blimey. The thread that refuses to die. . . .
~ Mansukh
Campbell Ritchie wrote:
Blimey. Page 3.Winston Gutkowski wrote: . . . Blimey. The thread that refuses to die. . . .
And no girls in sight
~ Mansukh
Campbell Ritchie wrote:Now we’re getting somewhere
Change that to read @throws IndexOutOfBoundsException if index < 0 or index > current size.Mansukhdeep Thind wrote: . . . /**
* @throws exception - {@link ArrayIndexOutOfBoundsException}
*/ . . .
Similarly for other @throws tags. You need to quote the class of the Exception immediately after the tag. You should check whether I have got that right here.
Look at your add(T) method. That can be simplified no end; you can actually reduce it to two statements, and use much simpler if statement. Why are you casting the element? When you have simplified that add method, you can similarly simplify the add(int, T) method.
And there is more to come.
~ Mansukh
No.Mansukhdeep Thind wrote: . . . Am I on correct track? . . .
Campbell Ritchie wrote:I often tell students, these things are a lot simpler than you think. You can simplify that add method and make it look elegant and short and concise. But you need to relax and work out what you really need in that method. And I said two statements, not two lines.
~ Mansukh
Campbell Ritchie wrote:You have already said under which circumstances you need to enlarge the array. Go back a page and you will find it.
Mansukhdeep Thind wrote: Also done. The array should grow when the size of the current list becomes == its capacity.
~ Mansukh
~ Mansukh
Yes, yes, yesMansukhdeep Thind wrote:Here you go Campbell:
The add(T ) method :
. . .
Campbell Ritchie wrote:You can elide the size++; line in add(int, T) similarly. In add(int, T), throw the Exception at the beginning of the method.
~ Mansukh
Campbell Ritchie wrote:You throw the Exception early in the method because it is obvious to readers what is happening. You can still improve that method, however. I can see two things I would alter, possibly three.
You are saying that you can add something at index 2 if the List contains "Campbell" "Manskukhdeep" only, in the documentation.
~ Mansukh
if(indexPos>size || indexPos<0){
System.out.println("Index passed must be between 0 and current size");
throw new IndexOutOfBoundsException();
}
~ Mansukh
Jeff Verdegan wrote:
if(indexPos>size || indexPos<0){
System.out.println("Index passed must be between 0 and current size");
throw new IndexOutOfBoundsException();
}
If you were writing this as a real library class that you expected to be used in multiple applications, possibly by people other than yourself, you wouldn't want to be calling System.out.println() anywhere in your code.
When throwing an exception, you should include useful information about what was expected and what happened instead, to aid debugging:
Also, these are more subjective, but personally, I hate seeing unnecessary uses of "this":
And more standard whitespace around binary operators and between the pieces of the for loop header and between the closing paren and opening brace will make it easier to read:
Campbell Ritchie wrote:Getting better all the time
Now let’s see a remove method.
~ Mansukh
So about 50 miles?Mansukhdeep Thind wrote: . . . An easy paced 3-4 hour work out. . . .
~ Mansukh
Campbell Ritchie wrote:Kilometers are French
~ Mansukh
Campbell Ritchie wrote:Getting better all the time
Now let’s see a remove method.
~ Mansukh
Campbell Ritchie wrote:There is a pitfall which you have fallen into. I would have fallen into it myself, had I not read Joshua Bloch’s Effective Java™ (2/e) page 24. (In the first edition it is page 16.)
~ Mansukh
Campbell Ritchie wrote:Why have you got the if inside the method still? Why are you checking for != null when removing? If you are not permitting nulls at this stage, why are you permitting users to add them in the add methods?
The temp variable is a local variable which goes out of scope when that method completes.
Write down the structure of your List after you add a few elements and remove one:
~ Mansukh
Campbell Ritchie wrote:If you are going to thrown an Exception, try NullPointerException.
Campbell Ritchie wrote:You have changed the class’ general contract. You used to permit null values and you don’t any more. Both options are valid, but you must decide which is correct and which you are using.
Campbell Ritchie wrote:Don’t use a plain simple Exception. If you are going to thrown an Exception, try NullPointerException. Also that way, you are throwing an unchecked Exception, which is appropriate for incorrect arguments.
Campbell Ritchie wrote:You have changed the return type of add(int, T); it used to be boolean, and it is now void (at least I think it was). I would have thought boolean as you used to have is better.
Campbell Ritchie wrote:Why are you casting the elements in the array to (T)?
Campbell Ritchie wrote:Have you printed out the no 3 element in the array? You are only printing up to < size. What happens when you print up to <= size?
~ Mansukh
You are right. By using i + 1 you have correctly copied the null in no 3 to no 2, so you are not getting the memory leak. I was mistaken about that. Sorry.Mansukhdeep Thind wrote: . . .
No. 3 element prints as null after removing the 3rd element. What are you getting at?
. . .
Jeff Verdegan wrote:
Campbell Ritchie wrote:If you are going to thrown an Exception, try NullPointerException.
I prefer IllegalArgumentException. Either one is valid, and it really comes down to personal preference. I prefer to let NPE only be thrown when a null pointer is actually dereferenced (or when I call into a library that throws it in situations like this, which I can't control ). I think IAE is more descriptive of what went wrong here. The fact that the argument was null doesn't make it an NPE, IMHO. Again, though, totally subjective. Pick whichever one you think fits better. The key points are that it should be more descriptive than Exception and it should be an unchecked exception.
~ Mansukh
Campbell Ritchie wrote:
You are right. By using i + 1 you have correctly copied the null in no 3 to no 2, so you are not getting the memory leak. I was mistaken about that. Sorry.Mansukhdeep Thind wrote: . . .
No. 3 element prints as null after removing the 3rd element. What are you getting at?
. . .
Campbell Ritchie wrote:Are you sure there is no chance that you will get an array out of bounds exception from remove(int) if size == values.length?
Campbell Ritchie wrote:If you are using void return type for add(), then change both add methods to the same return type. Then you can reduce add(T) to the dreaded two statements.
Campbell Ritchie wrote:You either have to cast the array when you create it, or cast each element as you extract it from the array. I would prefer the former, but java.util.ArrayList prefers the latter approach.
~ Mansukh
There’s no place like 127.0.0.1. But I'll always remember this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
|