Skip to content

Commit 80c2fdb

Browse files
committed
Implement partial list
1 parent e990e3b commit 80c2fdb

1 file changed

Lines changed: 7 additions & 4 deletions

File tree

‎java-core/src/main/java/com/sayemahmed/example/collection/CustomArrayList.java‎

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,11 @@ public class CustomArrayList<E> implements List<E> {
1313
private int nextIndex;
1414

1515
public CustomArrayList() {
16-
elements = new Object[1];
17-
nextIndex = 0;
16+
this(1);
17+
}
18+
19+
public CustomArrayList(int initialCapacity) {
20+
elements = new Object[initialCapacity];
1821
}
1922

2023
@Override
@@ -55,10 +58,10 @@ public E remove(int index) {
5558

5659
@Override
5760
public void add(int index, E element) {
58-
if (index < 0 || index > size()) {
61+
if (index < 0 || index > nextIndex) {
5962
throw new IndexOutOfBoundsException("index must be between 0 and " + size());
6063
}
61-
if (index == size()) {
64+
if (index == nextIndex) {
6265
add(element);
6366
return;
6467
}

0 commit comments

Comments
 (0)