-
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?
Changes from 4 commits
1a87a5d
939b3b6
58c52b9
98d410a
f2d3fa2
4ec2d52
030b08e
c7f9151
bea0209
2216abf
b5ad7f3
d89778c
a229576
6c9a798
c5bd73d
772eb65
833a81e
bfcc2ba
459bafa
0245dfd
45c2358
e20ed6f
83b79f5
611d5ff
388e486
bfd7fcb
5cf1b11
68bc970
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -1,48 +1,162 @@ | ||||||||||||||
package core.basesyntax; | ||||||||||||||
|
||||||||||||||
import java.util.NoSuchElementException; | ||||||||||||||
|
||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
|
||||||||||||||
public ArrayList() { | ||||||||||||||
array = (T[])new Object[10]; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 10 is magic number, should be extracted into constant.
Suggested change
|
||||||||||||||
lastElementIndex = -1; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
public ArrayList(T[] array) { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you need this constructor? |
||||||||||||||
this.array = array; | ||||||||||||||
lastElementIndex = array.length - 1; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public void add(T value) { | ||||||||||||||
|
||||||||||||||
if (lastElementIndex < array.length - 1) { | ||||||||||||||
++lastElementIndex; | ||||||||||||||
array[lastElementIndex] = value; | ||||||||||||||
} else { | ||||||||||||||
changeSize(1.5); | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 1.5 is a magic number, make it constant
Suggested change
|
||||||||||||||
add(value); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public void add(T value, int index) { | ||||||||||||||
|
||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. potentially repeating code, could be extracted into private method |
||||||||||||||
if (index == 0 && lastElementIndex < 0) { | ||||||||||||||
array[0] = value; | ||||||||||||||
++lastElementIndex; | ||||||||||||||
return; | ||||||||||||||
} | ||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. merge with previous out of bound check condition |
||||||||||||||
if (lastElementIndex == array.length - 1) { | ||||||||||||||
changeSize(1.5); | ||||||||||||||
} | ||||||||||||||
Object[] newArray = new Object[array.length]; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We do not need newArray in this method |
||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. consider using System.arraycopy to reduce the code |
||||||||||||||
array = (T[])newArray; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public void addAll(List<T> list) { | ||||||||||||||
|
||||||||||||||
int expectedLength = (list.size() + lastElementIndex + 1); | ||||||||||||||
if (expectedLength <= array.length) { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. invert IF condition and get rid of else word |
||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. consider reusing of add(value) method. |
||||||||||||||
} else { | ||||||||||||||
changeSize(1.5); | ||||||||||||||
addAll(list); | ||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public T get(int index) { | ||||||||||||||
return null; | ||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. 1 merge if statements into one |
||||||||||||||
return array[index]; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public void set(T value, int index) { | ||||||||||||||
|
||||||||||||||
if (index < 0) { | ||||||||||||||
throw new ArrayListIndexOutOfBoundsException("You're trying" | ||||||||||||||
+ " to set element, index of which is lower than 0"); | ||||||||||||||
} | ||||||||||||||
if (index > lastElementIndex) { | ||||||||||||||
throw new ArrayListIndexOutOfBoundsException("You're trying" | ||||||||||||||
+ " to set element, index of which is larger than array length"); | ||||||||||||||
} | ||||||||||||||
array[index] = value; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public T remove(int index) { | ||||||||||||||
return null; | ||||||||||||||
if (index < 0 || index > lastElementIndex) { | ||||||||||||||
throw new ArrayListIndexOutOfBoundsException("Invalid index, can't be removed"); | ||||||||||||||
} | ||||||||||||||
T temp = array[index]; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. |
||||||||||||||
lastElementIndex--; | ||||||||||||||
return temp; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public T remove(T element) { | ||||||||||||||
return null; | ||||||||||||||
int index = -1; | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. create separate method int index = index(element); There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
for (int i = 0; i <= lastElementIndex; ++i) { | ||||||||||||||
if (element == array[i]) { | ||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. Don't create repeating code. |
||||||||||||||
} | ||||||||||||||
} | ||||||||||||||
if (index == -1) { | ||||||||||||||
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 commentThe reason will be displayed to describe this comment to others. Learn more. include element in the message |
||||||||||||||
} | ||||||||||||||
remove(index); | ||||||||||||||
return element; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public int size() { | ||||||||||||||
return 0; | ||||||||||||||
return lastElementIndex + 1; | ||||||||||||||
} | ||||||||||||||
|
||||||||||||||
@Override | ||||||||||||||
public boolean isEmpty() { | ||||||||||||||
return false; | ||||||||||||||
if (lastElementIndex == -1) { | ||||||||||||||
return true; | ||||||||||||||
} else { | ||||||||||||||
return false; | ||||||||||||||
} | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Redundant if statement.
Suggested change
|
||||||||||||||
} | ||||||||||||||
|
||||||||||||||
private T[] changeSize(double coefficient) { | ||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||||||
int newLength = (int)(array.length * coefficient); | ||||||||||||||
Object[] tempArray = new Object[(int) newLength]; | ||||||||||||||
IvanIKS marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||
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 commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. not fixed |
||||||||||||||
return 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.