-
Notifications
You must be signed in to change notification settings - Fork 9
OOP hometask #9
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
OOP hometask #9
Conversation
| import java.util.Arrays; | ||
|
|
||
| public class ContainerImpl implements Container { | ||
| private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why Integer.MAX_VALUE - 8?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add a comment to code
| private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8; | ||
|
|
||
| private int size; | ||
| transient int[] elementData; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why transient? Are you going to serialize container?
| // TODO: please implement me | ||
| // clear to let GC do its work | ||
| for (int i = 0; i < size; i++) | ||
| elementData[i] = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please explain, why do you need it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any explanation why are you setting zero instead of removed elements?
| @Override | ||
| public boolean remove(int index) { | ||
| // TODO: please implement me | ||
| for (int i = 0; i < size; i++) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do you need loop here?
| grow(minCapacity); | ||
| } | ||
|
|
||
| private void grow(int minCapacity) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a hard to understand what you are doing here. Please add comments.
By the way, looks like copy-paste, so not sure you are aware what is going on.
|
|
||
| if (firstChar == "-".charAt(0)) { | ||
| negative = -1; | ||
| } else if (firstChar == "+".charAt(0)) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Empty condition looks strange.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed
| //TODO: implement me | ||
| if (value.length != 0 && value != null) { | ||
| char firstChar = value[0]; | ||
| int negative = 1; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not sure it's a good name. It's look weird to init variable named negative with positive integer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changed
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you remember equals+hashCode contract?
If objects are equal - hash code should be the same.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok
| } | ||
|
|
||
| @Override | ||
| public int hashCode() { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
| } | ||
|
|
||
| @Override | ||
| public boolean equals(Object o) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The same as above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed
| if (minCapacity - MAX_ARRAY_SIZE > 0) { | ||
| minCapacity = hugeCapacity(minCapacity); | ||
| } | ||
| elementData = Arrays.copyOf(elementData, minCapacity); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For sure it's not a good idea to increase array size by 1 each time new element is added.
Please use some multiplier.
| // TODO: please implement me | ||
| // clear to let GC do its work | ||
| for (int i = 0; i < size; i++) | ||
| elementData[i] = 0; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Any explanation why are you setting zero instead of removed elements?
| //TODO: implement me | ||
| int i; | ||
| if (value > 1) { | ||
| for (i = 2; i <= value / 2; i++) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not say you to put 6, I've just shown you that amount of checks can be significantly decreased.
Please analyse it one more time.
No description provided.