getElementAt
This commit is contained in:
@@ -106,7 +106,30 @@ public class Lista<E> {
|
||||
}
|
||||
}
|
||||
|
||||
// removeElementAt(int pos): rimuove il nodo in coda
|
||||
public E getElementAt(int posizione) throws IndexOutOfBoundsException, NoSuchElementException {
|
||||
if (posizione == 0) {
|
||||
if (this.testa == null) {
|
||||
throw new NoSuchElementException();
|
||||
} else {
|
||||
return this.testa.getInfo();
|
||||
}
|
||||
} else if (posizione == this.size - 1) {
|
||||
Nodo<E> coda = this.testa;
|
||||
while (coda.getNext() != null) {
|
||||
coda = coda.getNext();
|
||||
}
|
||||
return coda.getInfo();
|
||||
} else if (posizione < 0 || posizione >= this.size) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
} else {
|
||||
Nodo<E> corrente = this.testa;
|
||||
for (int i = 0; i < posizione; i++) {
|
||||
corrente = corrente.getNext();
|
||||
}
|
||||
return corrente.getInfo();
|
||||
}
|
||||
}
|
||||
|
||||
// tipoInfo getElementAt(int pos): restituisce l'info presente nel nodo in
|
||||
// posizione n (es. tipoInfo = char, tipoInfo = int)
|
||||
// String toString()
|
||||
|
||||
Reference in New Issue
Block a user