Compare commits
3 Commits
0295bde18c
...
fd72f740eb
| Author | SHA1 | Date |
|---|---|---|
|
|
fd72f740eb | |
|
|
9a98ed3770 | |
|
|
9eba905fc0 |
4
pom.xml
4
pom.xml
|
|
@ -27,8 +27,8 @@
|
||||||
<artifactId>maven-compiler-plugin</artifactId>
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
<version>3.8.1</version>
|
<version>3.8.1</version>
|
||||||
<configuration>
|
<configuration>
|
||||||
<source>1.8</source>
|
<source>17</source>
|
||||||
<target>1.8</target>
|
<target>17</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</plugin>
|
||||||
</plugins>
|
</plugins>
|
||||||
|
|
|
||||||
|
|
@ -1,16 +1,22 @@
|
||||||
package com.nbee.solution.practice;
|
package com.nbee.solution.practice;
|
||||||
|
|
||||||
public class DynamicArray {
|
import java.util.Arrays;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.function.Consumer;
|
||||||
|
import java.util.stream.IntStream;
|
||||||
|
|
||||||
|
public class DynamicArray implements Iterable<Integer> {
|
||||||
private int size = 0;
|
private int size = 0;
|
||||||
private int capacity = 8;
|
private int capacity = 8;
|
||||||
private int[] array = new int[capacity];
|
private int[] array = {};
|
||||||
|
|
||||||
public void addList(int element) {
|
public void addList(int element) {
|
||||||
add(size, element);
|
add(size, element);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void add(int index, int element) {
|
public void add(int index, int element) {
|
||||||
if (index < 0 || index > size){
|
checkAndGrow();
|
||||||
|
if (index < 0 || index > size) {
|
||||||
throw new IndexOutOfBoundsException();
|
throw new IndexOutOfBoundsException();
|
||||||
}
|
}
|
||||||
if (index >= 0 && index < size) {
|
if (index >= 0 && index < size) {
|
||||||
|
|
@ -20,14 +26,66 @@ public class DynamicArray {
|
||||||
size++;
|
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() {
|
public int getSize() {
|
||||||
return size;
|
return size;
|
||||||
}
|
}
|
||||||
|
|
||||||
public int get(int index) {
|
public int get(int index) {
|
||||||
if (index < 0 || index >= size){
|
if (index < 0 || index >= size) {
|
||||||
throw new IndexOutOfBoundsException();
|
throw new IndexOutOfBoundsException();
|
||||||
}
|
}
|
||||||
return array[index];
|
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,44 +1,79 @@
|
||||||
package com.nbee.solution.practice;
|
package com.nbee.solution.practice;
|
||||||
|
|
||||||
import org.junit.Assert;
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import static org.junit.jupiter.api.Assertions.*;
|
import java.util.List;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertIterableEquals;
|
||||||
|
|
||||||
class DynamicArrayTest {
|
class DynamicArrayTest {
|
||||||
|
|
||||||
private DynamicArray dynamicArray;
|
private DynamicArray dynamicArray;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
void setUp() {
|
public void setUp() {
|
||||||
dynamicArray = new DynamicArray();
|
dynamicArray = new DynamicArray();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAddListInitial() {
|
public void testAddListInitial() {
|
||||||
dynamicArray.addList(1);
|
dynamicArray.addList(1);
|
||||||
dynamicArray.addList(3);
|
dynamicArray.addList(3);
|
||||||
dynamicArray.addList(5);
|
dynamicArray.addList(5);
|
||||||
for (int i = 0; i < 3; i++){
|
dynamicArray.add(1, 7);
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
System.out.println(dynamicArray.get(i));
|
System.out.println(dynamicArray.get(i));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAddListMultiple() {
|
public void test01() {
|
||||||
dynamicArray.addList(2);
|
dynamicArray.addList(1);
|
||||||
dynamicArray.addList(3);
|
dynamicArray.addList(3);
|
||||||
dynamicArray.addList(4);
|
dynamicArray.addList(5);
|
||||||
assertEquals(3, dynamicArray.getSize(), "Size should be 3 after adding three elements.");
|
dynamicArray.add(1, 7);
|
||||||
|
dynamicArray.forEach(element -> {
|
||||||
|
System.out.println(element);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void testAddListCapacityExceeded() {
|
public void test02() {
|
||||||
for (int i = 0; i < 8; i++) {
|
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++) {
|
||||||
dynamicArray.addList(i);
|
dynamicArray.addList(i);
|
||||||
}
|
}
|
||||||
assertEquals(8, dynamicArray.getSize(), "Size should be 8 after adding elements up to capacity.");
|
System.out.println(dynamicArray.toString());
|
||||||
|
assertIterableEquals(List.of(0, 1, 2, 3, 4, 5, 6, 7, 8), dynamicArray, "List should be equal");
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue