Compare commits
No commits in common. "fd72f740ebe23b442e34135f91f4f64557445583" and "0295bde18ced874baf168e5abf662f035be77b2a" have entirely different histories.
fd72f740eb
...
0295bde18c
4
pom.xml
4
pom.xml
|
|
@ -27,8 +27,8 @@
|
|||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.8.1</version>
|
||||
<configuration>
|
||||
<source>17</source>
|
||||
<target>17</target>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
|
|
|||
|
|
@ -1,21 +1,15 @@
|
|||
package com.nbee.solution.practice;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
public class DynamicArray implements Iterable<Integer> {
|
||||
public class DynamicArray {
|
||||
private int size = 0;
|
||||
private int capacity = 8;
|
||||
private int[] array = {};
|
||||
private int[] array = new int[capacity];
|
||||
|
||||
public void addList(int element) {
|
||||
add(size, element);
|
||||
}
|
||||
|
||||
public void add(int index, int element) {
|
||||
checkAndGrow();
|
||||
if (index < 0 || index > size){
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
|
|
@ -26,66 +20,14 @@ public class DynamicArray implements Iterable<Integer> {
|
|||
size++;
|
||||
}
|
||||
|
||||
private void checkAndGrow() {
|
||||
if (size == 0) {
|
||||
array = new int[capacity];
|
||||
} else if (size == capacity) {
|
||||
capacity = capacity + (capacity >>> 1);
|
||||
int[] newArray = new int[capacity];
|
||||
System.arraycopy(array, 0, newArray, 0, size);
|
||||
array = newArray;
|
||||
}
|
||||
}
|
||||
|
||||
public int getSize() {
|
||||
return size;
|
||||
}
|
||||
|
||||
public int get(int index) {
|
||||
if (index < 0 || index >= size){
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
return array[index];
|
||||
}
|
||||
|
||||
public void forEach0(Consumer<Integer> consumer) {
|
||||
for (int i = 0; i < size; i++) {
|
||||
consumer.accept(array[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public Iterator<Integer> iterator() {
|
||||
return new Iterator<Integer>() {
|
||||
int i = 0;
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
return i < size;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Integer next() {
|
||||
return array[i++];
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
public IntStream stream() {
|
||||
return IntStream.of(Arrays.copyOfRange(array, 0, size));
|
||||
}
|
||||
|
||||
public int remove(int index) {
|
||||
int removed = array[index];
|
||||
if (index < size - 1) {
|
||||
System.arraycopy(array, index + 1, array, index, size - index - 1);
|
||||
size--;
|
||||
}
|
||||
return removed;
|
||||
}
|
||||
public String toString() {
|
||||
return Arrays.toString(Arrays.copyOfRange(array, 0, size));
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,79 +1,44 @@
|
|||
package com.nbee.solution.practice;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import java.util.List;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
|
||||
class DynamicArrayTest {
|
||||
|
||||
private DynamicArray dynamicArray;
|
||||
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
void setUp() {
|
||||
dynamicArray = new DynamicArray();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testAddListInitial() {
|
||||
void testAddListInitial() {
|
||||
dynamicArray.addList(1);
|
||||
dynamicArray.addList(3);
|
||||
dynamicArray.addList(5);
|
||||
dynamicArray.add(1, 7);
|
||||
for (int i = 0; i < 4; i++) {
|
||||
for (int i = 0; i < 3; i++){
|
||||
System.out.println(dynamicArray.get(i));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void test01() {
|
||||
dynamicArray.addList(1);
|
||||
void testAddListMultiple() {
|
||||
dynamicArray.addList(2);
|
||||
dynamicArray.addList(3);
|
||||
dynamicArray.addList(5);
|
||||
dynamicArray.add(1, 7);
|
||||
dynamicArray.forEach(element -> {
|
||||
System.out.println(element);
|
||||
});
|
||||
dynamicArray.addList(4);
|
||||
assertEquals(3, dynamicArray.getSize(), "Size should be 3 after adding three elements.");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test02() {
|
||||
dynamicArray.addList(1);
|
||||
dynamicArray.addList(3);
|
||||
dynamicArray.addList(5);
|
||||
dynamicArray.add(1, 7);
|
||||
for (Integer element : dynamicArray) {
|
||||
System.out.println(element);
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void test03() {
|
||||
dynamicArray.addList(1);
|
||||
dynamicArray.addList(3);
|
||||
dynamicArray.addList(5);
|
||||
dynamicArray.add(1, 7);
|
||||
dynamicArray.stream().forEach(element -> {
|
||||
System.out.println(element);
|
||||
});
|
||||
}
|
||||
@Test
|
||||
public void test04() {
|
||||
dynamicArray.addList(1);
|
||||
dynamicArray.addList(3);
|
||||
dynamicArray.addList(5);
|
||||
dynamicArray.add(1, 7);
|
||||
int removed =dynamicArray.remove(1);
|
||||
System.out.println("removed: " + removed);
|
||||
for (Integer element : dynamicArray) {
|
||||
System.out.println(element);
|
||||
}
|
||||
}
|
||||
@Test
|
||||
public void test05() {
|
||||
for (int i = 0; i < 9; i++) {
|
||||
void testAddListCapacityExceeded() {
|
||||
for (int i = 0; i < 8; i++) {
|
||||
dynamicArray.addList(i);
|
||||
}
|
||||
System.out.println(dynamicArray.toString());
|
||||
assertIterableEquals(List.of(0, 1, 2, 3, 4, 5, 6, 7, 8), dynamicArray, "List should be equal");
|
||||
assertEquals(8, dynamicArray.getSize(), "Size should be 8 after adding elements up to capacity.");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue