1
I’m learning from the Caelum datastructures handouts, and I started to see Liagadas Lists, followed all the code it offers, step by step and understood the logic of the chained list, but what I didn’t understand was this line of code:
System.out.println(lista); // lista é um objeto to tipo ListaLigada
Which outputs the string array:
[Paulo, Rafael]
I didn’t understand, because in my code the output you give is only of the class name + the object hascode. Can anyone explain what happened??? The whole code to avoid bureaucracy is there:
public class Celula {
private Celula proxima;
private Object elemento;
public Celula(Celula proxima, Object elemento) {
this.proxima = proxima;
this.elemento = elemento;
}
public Celula(Object elemento) {
this.elemento = elemento;
}
public void setProxima(Celula proxima) {
this.proxima = proxima;
}
public Celula getProxima() {
return proxima;
}
public Object getElemento() {
return elemento;
}
}
The Listed class on:
public class ListaLigada {
private Celula primeira;
private Celula ultima;
public void adiciona(Object elemento) {}
public void adiciona(int posicao, Object elemento) {}
public Object pega(int posicao) {return null;}
public void remove(int posicao){}
public int tamanho() {return 0;}
public boolean contem(Object o) {return false;}
}
The test code:
public class TesteAdicionaNoFim {
public static void main(String[] args) {
ListaLigada lista = new ListaLigada();
lista.adiciona("Rafael");
lista.adiciona("Paulo");
System.out.println(lista);
}
}
NOTE: I MADE THE SAME CODE, EXACTLY THE SAME. EACH LINE EXACTLY THE SAME WITH THE ONE OF CAELUM
This is wrong, his list is turned on, it is not a simple list, so it is not possible to scan simply, or it would have to have a method that does this or create an iterator within the class so that it can be used along with the
for
.– Maniero
So, ok... I ended up learning one more too! Rsrs Thanks.
– Rafael Silva