Skip to content

Conversation

@AnnaKlueva
Copy link

No description provided.

import java.util.Arrays;

public class ContainerImpl implements Container {
private static final int MAX_ARRAY_SIZE = Integer.MAX_VALUE - 8;
Copy link
Contributor

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?

Copy link
Author

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;
Copy link
Contributor

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;
Copy link
Contributor

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.

Copy link
Contributor

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++)
Copy link
Contributor

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) {
Copy link
Contributor

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)) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Empty condition looks strange.

Copy link
Author

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;
Copy link
Contributor

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.

Copy link
Author

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() {
Copy link
Contributor

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.

Copy link
Author

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() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as above.

Copy link
Author

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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The same as above.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@didva didva self-assigned this Jul 3, 2017
if (minCapacity - MAX_ARRAY_SIZE > 0) {
minCapacity = hugeCapacity(minCapacity);
}
elementData = Arrays.copyOf(elementData, minCapacity);
Copy link
Contributor

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;
Copy link
Contributor

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++) {
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants