Skip to content
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

Can you check my task, please? #1214

Open
wants to merge 28 commits into
base: master
Choose a base branch
from
Open

Conversation

IvanIKS
Copy link

@IvanIKS IvanIKS commented Nov 6, 2023

No description provided.

private int lastElementIndex;

public ArrayList() {
array = (T[])new Object[10];
Copy link

Choose a reason for hiding this comment

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

10 is magic number, should be extracted into constant.

Suggested change
array = (T[])new Object[10];
array = (T[])new Object[CONSTANT];

++lastElementIndex;
array[lastElementIndex] = value;
} else {
changeSize(1.5);
Copy link

Choose a reason for hiding this comment

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

1.5 is a magic number, make it constant

Suggested change
changeSize(1.5);
changeSize(CONSTANT);

Comment on lines 32 to 35
if (index < 0) {
throw new ArrayListIndexOutOfBoundsException("Invalid index, can't be added to"
+ " non-existing position.");
}
Copy link

Choose a reason for hiding this comment

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

potentially repeating code, could be extracted into private method

Comment on lines 41 to 44
if (index > lastElementIndex + 1) {
throw new ArrayListIndexOutOfBoundsException("Invalid index, can't be added to"
+ " non-existing position.");
}
Copy link

Choose a reason for hiding this comment

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

merge with previous out of bound check condition

Comment on lines 48 to 56
Object[] newArray = new Object[array.length];
for (int i = 0; i < index; ++i) {
newArray[i] = array[i];
}
newArray[index] = value;
for (int i = index + 1; i <= lastElementIndex + 1; ++i) {
newArray[i] = array[i - 1];
}
++lastElementIndex;
Copy link

Choose a reason for hiding this comment

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

consider using System.arraycopy to reduce the code

Comment on lines 64 to 67
for (int i = 0; i < list.size(); ++i) {
++lastElementIndex;
array[lastElementIndex] = list.get(i);
}
Copy link

Choose a reason for hiding this comment

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

consider reusing of add(value) method.

Comment on lines 76 to 83
if (index < 0) {
throw new ArrayListIndexOutOfBoundsException("You're trying"
+ " to get element, index of which is lower than 0");
}
if (index > lastElementIndex) {
throw new ArrayListIndexOutOfBoundsException("You're trying"
+ " to get element, index of which is larger than array length.");
}
Copy link

Choose a reason for hiding this comment

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

1 merge if statements into one
2 extract private method for reuse purpose

Comment on lines 140 to 144
if (lastElementIndex == -1) {
return true;
} else {
return false;
}
Copy link

Choose a reason for hiding this comment

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

Redundant if statement. lastElementIndex == -1 already returns boolean

Suggested change
if (lastElementIndex == -1) {
return true;
} else {
return false;
}
return lastElementIndex == -1

Comment on lines 150 to 159
if (newLength > array.length) {
for (int i = 0; i < array.length; i++) {
tempArray[i] = array[i];
}
} else if (newLength < array.length) {
for (int i = 0; i < newLength; i++) {
tempArray[i] = array[i];
}
}
array = (T[]) tempArray;
Copy link

Choose a reason for hiding this comment

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

consider using System.arraycopy

Copy link
Contributor

Choose a reason for hiding this comment

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

not fixed

@IvanIKS IvanIKS requested a review from sokimaaa November 8, 2023 19:18
@IvanIKS
Copy link
Author

IvanIKS commented Nov 8, 2023

I made changes you requested. Can you check it, please?

Comment on lines 8 to 9
private final int baseSize = 10;
private final double enlargingSize = 1.5;
Copy link
Contributor

Choose a reason for hiding this comment

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

image

public class ArrayList<T> implements List<T> {
private T[] array;
private int lastElementIndex;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
private int lastElementIndex;
private int size;


public ArrayList() {
array = (T[])new Object[baseSize];
lastElementIndex = -1;
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
lastElementIndex = -1;

if (lastElementIndex == array.length - 1) {
changeSize(enlargingSize);
}
Object[] newArray = new Object[array.length];
Copy link
Contributor

Choose a reason for hiding this comment

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

We do not need newArray in this method

}

@Override
public void addAll(List<T> list) {

int expectedLength = (list.size() + this.size());
if (expectedLength <= array.length) {
Copy link
Contributor

Choose a reason for hiding this comment

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

invert IF condition and get rid of else word

Comment on lines 82 to 84
for (int i = index; i < lastElementIndex; ++i) {
array[i] = array[i + 1];
}
Copy link
Contributor

Choose a reason for hiding this comment

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

image

Comment on lines 94 to 98
index = i;
break;
} else if (element != null && element.equals(array[i])) {
index = i;
break;
Copy link
Contributor

Choose a reason for hiding this comment

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

Don't create repeating code.

Comment on lines 150 to 159
if (newLength > array.length) {
for (int i = 0; i < array.length; i++) {
tempArray[i] = array[i];
}
} else if (newLength < array.length) {
for (int i = 0; i < newLength; i++) {
tempArray[i] = array[i];
}
}
array = (T[]) tempArray;
Copy link
Contributor

Choose a reason for hiding this comment

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

not fixed

return array;
}

private void checkIfArgumentIsCorrect(int argument) {
Copy link
Contributor

Choose a reason for hiding this comment

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

unclear method name

Suggested change
private void checkIfArgumentIsCorrect(int argument) {
private void checkIndex(int index) {

@IvanIKS IvanIKS requested a review from Rommelua April 23, 2024 22:10
public class ArrayList<T> implements List<T> {
private T[] array;
private int size;
private final int baseSize = 10;

Choose a reason for hiding this comment

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

constant

private T[] array;
private int size;
private final int baseSize = 10;
private final double enlargingSize = 1.5;

Choose a reason for hiding this comment

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

not fixed


public ArrayList() {
array = (T[]) new Object[baseSize];
size = 0;

Choose a reason for hiding this comment

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

int type = 0 by default

size = 0;
}

public ArrayList(T[] array) {

Choose a reason for hiding this comment

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

Why do you need this constructor?

public class ArrayList<T> implements List<T> {
private T[] array;

Choose a reason for hiding this comment

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

Suggested change
private T[] array;
private T[] elements;

}

private T[] changeSize(double coefficient) {
int newLength = (int) (array.length * coefficient);

Choose a reason for hiding this comment

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

use CAPACITY_INDEX instead of coefficient

private T[] array;
private int size;
private final int baseSize = 10;
private final double enlargingSize = 1.5;

Choose a reason for hiding this comment

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

Suggested change
private final double enlargingSize = 1.5;
private static final double CAPACITY_INDEX = 1.5;

src/main/java/core/basesyntax/ArrayList.java Outdated Show resolved Hide resolved
}

private void checkIndex(int index) {
if (index < 0) {

Choose a reason for hiding this comment

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

Suggested change
if (index < 0) {
if (index < 0 || index >= size) {
throw new ArrayListIndexOutOfBoundsException("There is no such index in the list, "
+ index);
}

if (index > size - 1) {
throw new ArrayListIndexOutOfBoundsException("You're trying"
+ " to access element, index of which is larger than array length.");
}

Choose a reason for hiding this comment

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

add

private void checkIndex(int index) {
        if (index < 0 || index > size) {
            throw new ArrayListIndexOutOfBoundsException("Invalid index, " + index);
        }
    }

@IvanIKS IvanIKS requested a review from Elena-Bruyako April 25, 2024 08:14
Copy link
Contributor

@Rommelua Rommelua left a comment

Choose a reason for hiding this comment

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

Previous comments are not fixed

@IvanIKS IvanIKS requested a review from Rommelua August 3, 2024 10:39
int index = -1;
for (int i = 0; i < size; ++i) {
if (element == elements[i]
| (element != null && element.equals(elements[i]))) {
Copy link

Choose a reason for hiding this comment

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

Suggested change
| (element != null && element.equals(elements[i]))) {
|| (element != null && element.equals(elements[i]))) {

always use short-circuiting operators

Comment on lines 111 to 119
throw new ArrayListIndexOutOfBoundsException("There is no such element in the list.");
}
}

private void checkAddIndex(int index) {
if (index < 0 || index > size + 1) {
throw new ArrayListIndexOutOfBoundsException("Invalid index, can't add element to"
+ " non-existing position.");
}
Copy link

Choose a reason for hiding this comment

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

include index and size in the message

Comment on lines 85 to 86
throw new NoSuchElementException("You're trying to remove element,"
+ " that is not present in the list");
Copy link

Choose a reason for hiding this comment

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

include element in the message

@IvanIKS IvanIKS requested a review from okuzan August 4, 2024 20:20
# Conflicts:
#	src/main/java/core/basesyntax/ArrayList.java
Comment on lines 26 to 39
checkAddIndex(index);
if (index == 0 && size == 0) {
add(value);
return;
}
if (size >= elements.length) {
resize();
}
size++;
T[] temp = (T[]) (new Object[elements.length]);
System.arraycopy(elements, 0, temp, 0, index);
temp[index] = value;
System.arraycopy(elements, index, temp, index + 1, elements.length - index - 1);
elements = (T[]) temp; //without this array does not resize properly.

Choose a reason for hiding this comment

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

Suggested change
checkAddIndex(index);
if (index == 0 && size == 0) {
add(value);
return;
}
if (size >= elements.length) {
resize();
}
size++;
T[] temp = (T[]) (new Object[elements.length]);
System.arraycopy(elements, 0, temp, 0, index);
temp[index] = value;
System.arraycopy(elements, index, temp, index + 1, elements.length - index - 1);
elements = (T[]) temp; //without this array does not resize properly.
checkAddIndex(index);
resize();
System.arraycopy(elements, index, elements, index + 1, size - index);
elements[index] = value;
size++;

Comment on lines 17 to 19
if (size >= elements.length) {
resize();
}

Choose a reason for hiding this comment

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

Suggested change
if (size >= elements.length) {
resize();
}
resize();

}

@Override
public T remove(T element) {
return null;
int index = -1;

Choose a reason for hiding this comment

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

Suggested change
int index = -1;
int index = index(element);
if (index == -1) {
throw new NoSuchElementException("There is no such element in the list, " + element);
}
return remove(index);

return size == 0;
}

private void resize() {

Choose a reason for hiding this comment

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

Suggested change
private void resize() {
private void resize() {
if (elements.length == size) {...

@IvanIKS IvanIKS requested a review from Elena-Bruyako August 6, 2024 19:39

private int findElement(T element) {
for (int i = 0; i < size; ++i) {
if (Objects.equals(element, elements[i])) {

Choose a reason for hiding this comment

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

}

private void checkAddIndex(int index) {
if (index < 0 || index > size + 1) {

Choose a reason for hiding this comment

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

Suggested change
if (index < 0 || index > size + 1) {
if (index < 0 || index > size) {

@IvanIKS
Copy link
Author

IvanIKS commented Aug 7, 2024

Sorry, Intellij Idea told me to replace my longer "element == <...>" with Object.equals. I changed back. Can you check again now, please?

@IvanIKS IvanIKS requested a review from Elena-Bruyako August 7, 2024 14:59
Copy link

@okuzan okuzan left a comment

Choose a reason for hiding this comment

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

On real job, you will use Objects.equals, it's forbidden to use in these homework (up to hashmap) only to teach students how to write the code themselves, good job

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.

6 participants