Commit 803d7024 authored by Yngve Sekse Kristiansen's avatar Yngve Sekse Kristiansen
Browse files

Added generics (OOPS)

parent d48fe1b0
......@@ -2,7 +2,7 @@ package inf101.v19.datastructures;
import inf101.v19.cell.CellState;
public interface IList {
public interface IList<T> {
/**
* Legg til et element på slutten av listen.
*
......@@ -10,7 +10,7 @@ public interface IList {
*
* Etterpå vil size() øke med én, og get(size()-1) vil returnere elementet.
*/
void add(CellState s);
void add(T s);
/**
* Fjern et element fra listen
......@@ -20,14 +20,14 @@ public interface IList {
*
* Etterpå vil alle senere indekser i listen flyttes én posisjon frem.
*/
CellState remove(int i);
T remove(int i);
/**
* Returner elementet på posisjon i
* @param i
* @return
*/
CellState get(int i);
T get(int i);
/**
* @return True hvis listen er tom
......@@ -47,7 +47,7 @@ public interface IList {
*
* Etterpå vil get(i) == s
*/
void set(int i, CellState s);
void set(int i, T s);
/**
* Fjern alle elementer fra listen.
......
......@@ -2,26 +2,26 @@ package inf101.v19.datastructures;
import java.util.Arrays;
import inf101.v19.cell.CellState;
public class MyList implements IList {
public class MyList<T> implements IList<T> {
private int length;
private CellState[] data;
private T[] data;
@SuppressWarnings("unchecked")
public MyList() {
length = 0;
data = new CellState[10];
data = (T[]) new Object[10];
}
@SuppressWarnings("unchecked")
public MyList(int initialSize) {
if(initialSize < 0)
throw new IllegalArgumentException("initialSize must be positive: " + initialSize);
length = 0;
data = new CellState[initialSize];
data = (T[]) new Object[initialSize];
}
@Override
public void add(CellState s) {
public void add(T s) {
if (length == data.length) {
data = Arrays.copyOf(data, data.length * 2);
}
......@@ -31,8 +31,8 @@ public class MyList implements IList {
}
@Override
public CellState remove(int i) {
CellState element = data[i];
public T remove(int i) {
T element = data[i];
for (int x = i; x < length - 1; x++) {
data[x] = data[x + 1];
......@@ -43,7 +43,7 @@ public class MyList implements IList {
}
@Override
public CellState get(int i) {
public T get(int i) {
return data[i];
}
......@@ -58,7 +58,7 @@ public class MyList implements IList {
}
@Override
public void set(int i, CellState s) {
public void set(int i, T s) {
data[i] = s;
}
......@@ -67,4 +67,4 @@ public class MyList implements IList {
length = 0;
}
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment