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 8 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
115 changes: 106 additions & 9 deletions src/main/java/core/basesyntax/ArrayList.java
Original file line number Diff line number Diff line change
@@ -1,48 +1,145 @@
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;

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

Choose a reason for hiding this comment

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

not fixed

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;


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;

}

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(enlargingSize);
add(value);
}
}

@Override
public void add(T value, int index) {

if (index < 0 || index > lastElementIndex + 1) {
throw new ArrayListIndexOutOfBoundsException("Invalid index, can't be added to"
+ " non-existing position.");
}
if (index == 0 && lastElementIndex < 0) {
add(value);
return;
}
++lastElementIndex;
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

System.arraycopy(array, 0, newArray, 0, index);
newArray[index] = value;
System.arraycopy(array, index, newArray, index + 1, array.length - index - 1);
array = (T[])newArray;
}

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

int expectedLength = (list.size() + this.size());

Choose a reason for hiding this comment

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

Suggested change
int expectedLength = (list.size() + this.size());
for (int i = 0; i < list.size(); i++) {
resize();
elements[size] = list.get(i);
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

for (int i = 0; i < list.size(); ++i) {
add(list.get(i));
}
} else {
changeSize(enlargingSize);
addAll(list);
}
}

@Override
public T get(int index) {
return null;
checkIfArgumentIsCorrect(index);
return array[index];
}

@Override
public void set(T value, int index) {

checkIfArgumentIsCorrect(index);
array[index] = value;
}

@Override
public T remove(int index) {
return null;
checkIfArgumentIsCorrect(index);
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;
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;
}

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

if (argument < 0) {
throw new ArrayListIndexOutOfBoundsException("You're trying"
+ " to access element, index of which is lower than 0");
}
if (argument > lastElementIndex) {
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);
        }
    }

}
}
Loading