-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
base: master
Are you sure you want to change the base?
Conversation
private int lastElementIndex; | ||
|
||
public ArrayList() { | ||
array = (T[])new Object[10]; |
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.
10 is magic number, should be extracted into constant.
array = (T[])new Object[10]; | |
array = (T[])new Object[CONSTANT]; |
++lastElementIndex; | ||
array[lastElementIndex] = value; | ||
} else { | ||
changeSize(1.5); |
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.
1.5 is a magic number, make it constant
changeSize(1.5); | |
changeSize(CONSTANT); |
if (index < 0) { | ||
throw new ArrayListIndexOutOfBoundsException("Invalid index, can't be added to" | ||
+ " non-existing position."); | ||
} |
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.
potentially repeating code, could be extracted into private method
if (index > lastElementIndex + 1) { | ||
throw new ArrayListIndexOutOfBoundsException("Invalid index, can't be added to" | ||
+ " non-existing position."); | ||
} |
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.
merge with previous out of bound check condition
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; |
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.
consider using System.arraycopy to reduce the code
for (int i = 0; i < list.size(); ++i) { | ||
++lastElementIndex; | ||
array[lastElementIndex] = list.get(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.
consider reusing of add(value) method.
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."); | ||
} |
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.
1 merge if statements into one
2 extract private method for reuse purpose
if (lastElementIndex == -1) { | ||
return true; | ||
} else { | ||
return false; | ||
} |
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.
Redundant if statement. lastElementIndex == -1
already returns boolean
if (lastElementIndex == -1) { | |
return true; | |
} else { | |
return false; | |
} | |
return lastElementIndex == -1 |
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; |
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.
consider using System.arraycopy
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 fixed
I made changes you requested. Can you check it, please? |
private final int baseSize = 10; | ||
private final double enlargingSize = 1.5; |
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.
public class ArrayList<T> implements List<T> { | ||
private T[] array; | ||
private int lastElementIndex; |
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.
private int lastElementIndex; | |
private int size; |
|
||
public ArrayList() { | ||
array = (T[])new Object[baseSize]; | ||
lastElementIndex = -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.
lastElementIndex = -1; |
if (lastElementIndex == array.length - 1) { | ||
changeSize(enlargingSize); | ||
} | ||
Object[] newArray = new Object[array.length]; |
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.
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) { |
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.
invert IF condition and get rid of else word
for (int i = index; i < lastElementIndex; ++i) { | ||
array[i] = array[i + 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.
index = i; | ||
break; | ||
} else if (element != null && element.equals(array[i])) { | ||
index = i; | ||
break; |
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.
Don't create repeating code.
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; |
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 fixed
return array; | ||
} | ||
|
||
private void checkIfArgumentIsCorrect(int argument) { |
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.
unclear method name
private void checkIfArgumentIsCorrect(int argument) { | |
private void checkIndex(int index) { |
public class ArrayList<T> implements List<T> { | ||
private T[] array; | ||
private int size; | ||
private final int baseSize = 10; |
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.
constant
private T[] array; | ||
private int size; | ||
private final int baseSize = 10; | ||
private final double enlargingSize = 1.5; |
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 fixed
|
||
public ArrayList() { | ||
array = (T[]) new Object[baseSize]; | ||
size = 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.
int type = 0 by default
size = 0; | ||
} | ||
|
||
public ArrayList(T[] array) { |
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 this constructor?
public class ArrayList<T> implements List<T> { | ||
private T[] array; |
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.
private T[] array; | |
private T[] elements; |
} | ||
|
||
private T[] changeSize(double coefficient) { | ||
int newLength = (int) (array.length * coefficient); |
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.
use CAPACITY_INDEX instead of coefficient
private T[] array; | ||
private int size; | ||
private final int baseSize = 10; | ||
private final double enlargingSize = 1.5; |
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.
private final double enlargingSize = 1.5; | |
private static final double CAPACITY_INDEX = 1.5; |
} | ||
|
||
private void checkIndex(int index) { | ||
if (index < 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.
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."); | ||
} |
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
private void checkIndex(int index) {
if (index < 0 || index > size) {
throw new ArrayListIndexOutOfBoundsException("Invalid index, " + index);
}
}
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.
Previous comments are not fixed
int index = -1; | ||
for (int i = 0; i < size; ++i) { | ||
if (element == elements[i] | ||
| (element != null && element.equals(elements[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.
| (element != null && element.equals(elements[i]))) { | |
|| (element != null && element.equals(elements[i]))) { |
always use short-circuiting operators
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."); | ||
} |
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.
include index and size in the message
throw new NoSuchElementException("You're trying to remove element," | ||
+ " that is not present in the list"); |
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.
include element in the message
# Conflicts: # src/main/java/core/basesyntax/ArrayList.java
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. |
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.
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++; |
if (size >= elements.length) { | ||
resize(); | ||
} |
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.
if (size >= elements.length) { | |
resize(); | |
} | |
resize(); |
} | ||
|
||
@Override | ||
public T remove(T element) { | ||
return null; | ||
int index = -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.
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() { |
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.
private void resize() { | |
private void resize() { | |
if (elements.length == size) {... |
# Conflicts: # src/main/java/core/basesyntax/ArrayList.java
|
||
private int findElement(T element) { | ||
for (int i = 0; i < size; ++i) { | ||
if (Objects.equals(element, elements[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.
} | ||
|
||
private void checkAddIndex(int index) { | ||
if (index < 0 || index > size + 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.
if (index < 0 || index > size + 1) { | |
if (index < 0 || index > size) { |
Sorry, Intellij Idea told me to replace my longer "element == <...>" with Object.equals. I changed back. Can you check again now, please? |
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.
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
No description provided.