B2: remove & indexOf implementiert

This commit is contained in:
Johannes Theiner 2018-11-16 16:39:58 +01:00
parent eeb322db2e
commit 6a57d69524
5 changed files with 74 additions and 7 deletions

View File

@ -49,6 +49,11 @@ public class ArrayList<E> implements List<E> {
return false; return false;
} }
@Override
public int indexOf(E e) {
return 0;
}
@NotNull @NotNull
public Iterator<E> iterator() { public Iterator<E> iterator() {
return new Iterator<E>() { return new Iterator<E>() {

View File

@ -82,6 +82,11 @@ public class LinkedList<E> implements List<E> {
return false; return false;
} }
@Override
public int indexOf(E e) {
return 0;
}
@NotNull @NotNull
public Iterator<E> iterator() { public Iterator<E> iterator() {
return new Iterator<E>() { return new Iterator<E>() {

View File

@ -7,4 +7,5 @@ public interface List<E> extends Iterable<E>{
int size(); int size();
E remove(int index); E remove(int index);
boolean remove(Object o); boolean remove(Object o);
int indexOf(E e);
} }

View File

@ -35,7 +35,7 @@ public class RingList<E> implements List<E> {
return true; return true;
} }
public E get(int index) { private Wrapper getWrapper(int index) {
Wrapper wRun; Wrapper wRun;
if (index < 0 || index >= size) { if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(); throw new IndexOutOfBoundsException();
@ -44,7 +44,11 @@ public class RingList<E> implements List<E> {
for (int iRun = 0; iRun < index; iRun++) { for (int iRun = 0; iRun < index; iRun++) {
wRun = wRun.succ; wRun = wRun.succ;
} }
return wRun.e; return wRun;
}
public E get(int index) {
return getWrapper(index).e;
} }
public int size() { public int size() {
@ -53,14 +57,41 @@ public class RingList<E> implements List<E> {
@Override @Override
public E remove(int index) { public E remove(int index) {
return null; Wrapper wrapper = getWrapper(index - 1);
wrapper.succ = wrapper.succ.succ;
size--;
return wrapper.succ.e;
} }
@Override @Override
public boolean remove(Object o) { public boolean remove(Object o) {
try {
int index = indexOf((E) o);
if(index == -1) return false;
remove(index);
return true;
} catch (ClassCastException ex) {
ex.printStackTrace();
}
return false; return false;
} }
@Override
public int indexOf(E e) {
Wrapper wrapper = this.first.succ;
for (int i = 0; i < size; i++) {
if(wrapper.e.equals(e)) {
return i;
}else wrapper = wrapper.succ;
}
return -1;
}
@NotNull @NotNull
public Iterator<E> iterator() { public Iterator<E> iterator() {
return new Iterator<E>() { return new Iterator<E>() {

View File

@ -1,6 +1,5 @@
package xyz.joethei.studium.algodat.vorlesung; package xyz.joethei.studium.algodat.vorlesung;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import xyz.joethei.studium.algodat.praktikum.blatt2.List; import xyz.joethei.studium.algodat.praktikum.blatt2.List;
import xyz.joethei.studium.algodat.praktikum.blatt2.RingList; import xyz.joethei.studium.algodat.praktikum.blatt2.RingList;
@ -8,6 +7,7 @@ import xyz.joethei.studium.algodat.praktikum.blatt2.RingList;
import java.util.Optional; import java.util.Optional;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
class Blatt2Test { class Blatt2Test {
@ -39,17 +39,42 @@ class Blatt2Test {
assertEquals(10, rList.size()); assertEquals(10, rList.size());
} }
@Disabled
@Test @Test
void remove() { void removeIndex() {
List<Integer> rList = new RingList<>(); List<Integer> rList = new RingList<>();
for (int i = 1; i <= 10; i++) { for (int i = 1; i <= 10; i++) {
rList.add(i * i); rList.add(i * i);
} }
assertEquals(Optional.of(36), Optional.ofNullable(rList.remove(5))); assertEquals(Optional.of(49), Optional.ofNullable(rList.remove(5)));
assertEquals(9, rList.size()); assertEquals(9, rList.size());
assertTrue(rList.get(5) != 36); assertTrue(rList.get(5) != 36);
} }
@Test
void removeObject() {
List<Integer> rList = new RingList<>();
for (int i = 1; i <= 10; i++) {
rList.add(i * i);
}
assertTrue(rList.remove(Integer.valueOf(9)));
assertFalse(rList.remove(Integer.valueOf(5)));
assertEquals(9, rList.size());
assertEquals(-1, rList.indexOf(9));
}
@Test
void indexOf() {
List<Integer> rList = new RingList<>();
for (int i = 1; i <= 10; i++) {
rList.add(i * i);
}
assertEquals(0, rList.indexOf(1));
assertEquals(1, rList.indexOf(4));
assertEquals(2, rList.indexOf(9));
assertEquals(3, rList.indexOf(16));
}
} }