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
Changes from 4 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
1a87a5d
update checklist.md (#1182)
boroda4436 Oct 12, 2023
939b3b6
Merge remote-tracking branch 'origin/master'
IvanIKS Nov 6, 2023
58c52b9
Merge remote-tracking branch 'origin/master'
IvanIKS Nov 6, 2023
98d410a
Merge remote-tracking branch 'origin/master'
IvanIKS Nov 6, 2023
f2d3fa2
Merge remote-tracking branch 'origin/master'
IvanIKS Nov 6, 2023
4ec2d52
Merge remote-tracking branch 'origin/master'
IvanIKS Nov 8, 2023
030b08e
Merge remote-tracking branch 'origin/master'
IvanIKS Nov 8, 2023
c7f9151
Merge remote-tracking branch 'origin/master'
IvanIKS Nov 8, 2023
bea0209
Merge remote-tracking branch 'origin/master'
IvanIKS Nov 8, 2023
2216abf
Merge remote-tracking branch 'origin/master'
IvanIKS Apr 23, 2024
b5ad7f3
Merge remote-tracking branch 'origin/master'
IvanIKS Apr 23, 2024
d89778c
Merge remote-tracking branch 'origin/master'
IvanIKS Apr 23, 2024
a229576
Merge remote-tracking branch 'origin/master'
IvanIKS Apr 23, 2024
6c9a798
Merge remote-tracking branch 'origin/master'
IvanIKS Apr 24, 2024
c5bd73d
Fixed problems.
IvanIKS Apr 24, 2024
772eb65
Merge remote-tracking branch 'origin/master'
IvanIKS Apr 28, 2024
833a81e
Merge remote-tracking branch 'origin/master'
IvanIKS Aug 3, 2024
bfcc2ba
Merge remote-tracking branch 'origin/master'
IvanIKS Aug 4, 2024
459bafa
Merge remote-tracking branch 'origin/master'
IvanIKS Aug 4, 2024
0245dfd
Merge remote-tracking branch 'origin/master'
IvanIKS Aug 4, 2024
45c2358
Merge remote-tracking branch 'origin/master'
IvanIKS Aug 4, 2024
e20ed6f
Merge remote-tracking branch 'origin/master'
IvanIKS Aug 6, 2024
83b79f5
Merge remote-tracking branch 'origin/master'
IvanIKS Aug 6, 2024
611d5ff
Merge remote-tracking branch 'origin/master'
IvanIKS Aug 6, 2024
388e486
Merge remote-tracking branch 'origin/master'
IvanIKS Aug 6, 2024
bfd7fcb
Merge remote-tracking branch 'origin/master'
IvanIKS Aug 6, 2024
5cf1b11
Merge remote-tracking branch 'origin/master'
IvanIKS Aug 7, 2024
68bc970
Merge remote-tracking branch 'origin/master'
IvanIKS Aug 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
132 changes: 123 additions & 9 deletions src/main/java/core/basesyntax/ArrayList.java
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;

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 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[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 = -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;

}

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?

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);
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);

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.");
}
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

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.");
}
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

if (lastElementIndex == array.length - 1) {
changeSize(1.5);
}
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

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

array = (T[])newArray;
}

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

int expectedLength = (list.size() + lastElementIndex + 1);
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

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.

} 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.");
}
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

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];

Choose a reason for hiding this comment

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

Suggested change
T temp = array[index];
T removedElement = array[index];

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

lastElementIndex--;
return temp;
}

@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.

create separate method int index = index(element);

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);

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;
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.

}
}
if (index == -1) {
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

}
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;
}
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

}

private T[] changeSize(double coefficient) {

Choose a reason for hiding this comment

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

Suggested change
private T[] changeSize(double coefficient) {
private void resize() {

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;
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

return array;
}
}
Loading