Java List Implementation with Search, Insert, and Remove
Classified in Technology
Written on in English with a size of 2.3 KB
Java List Implementation
Class Definition
public class Lista {
private Object[] item;
private int primeiro, ultimo, pos;
public Lista(int maxTam) {
this.item = new Object[maxTam];
this.pos = -1;
this.primeiro = 0;
this.ultimo = this.primeiro;
}
}
Search Method
public Object pesquisa(Object chave) {
if (this.vazia() || chave == null) {
return null;
}
for (int p = 0; p < this.ultimo; p++) {
if (this.item[p].equals(chave)) {
return this.item[p];
}
}
return null;
}
Insert Method
public void insere(Object x) {
if (this.ultimo >= this.item.length) {
throw new Exception("Error: List is full");
} else {
this.item[this.ultimo]