Problems with a Java Linked List object

Asked

Viewed 430 times

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

2 answers

1

I can’t guarantee that the code was actually typed exactly the same. If it was and the intention was to do what is in the question then the booklet is wrong, which does not surprise me where it came from.

When you have the object printed it is something like this ListaLigada@52e922 even if you receive.

To list the elements you need to create a method that lists them. Did not leave as exercise for you?

Some people liked to overwrite the ToString() to do this, I think abuse of the resource.

Note that even if this problem was solved the method adiciona() does nothing, so does not solve. This class is incomplete.

The correct solution would be to create an iterator (through the interface Iterable, but this is too complex for those who are starting and I doubt that a booklet is intended to teach anything beyond the basics.

1

To print a list in Java you have to go through it. Here is an example:

for (String nome : lista){
    System.out.println(nome);
}

The issue of printing the list will be resolved. But as the friend said above, you have to review the contents of this booklet.

  • 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.

  • So, ok... I ended up learning one more too! Rsrs Thanks.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.