getElementAt

This commit is contained in:
La Programmatrice Verde
2026-03-24 09:33:50 +01:00
parent 31405075b0
commit 34a3602b0d

View File

@@ -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 // tipoInfo getElementAt(int pos): restituisce l'info presente nel nodo in
// posizione n (es. tipoInfo = char, tipoInfo = int) // posizione n (es. tipoInfo = char, tipoInfo = int)
// String toString() // String toString()