How do I add a different object to a list every time I use the add?

3

I'm having trouble adding a different object to a list of objects every time I use the add of it, I know the problem but I do not know how to fix it, follow the code Class list

public class Lista {

    NodeLista inicio;
    NodeLista fim;

    public Lista (){
        inicio = null;
        fim = null;
    }

    public boolean isEmpty(){
        return inicio == null;
    }

    public int size(){
        if(isEmpty()){
            return 0;
        }
        NodeLista aux = inicio;
        int cont = 1;
        while(aux.proximo != inicio){
            cont++;
            aux = aux.proximo;
            if(aux == inicio){
                break;
            }
        }
        return cont;
    }

    public void add(NodeTree arvore){
        NodeLista novo = new NodeLista(arvore);
        if(isEmpty()){
            inicio = novo;
            fim = novo;
            inicio.proximo = null;
            fim.proximo = null;
        }else {
             fim.proximo = novo;
             fim = novo;
             fim.proximo = null;


        }
    }

    public boolean validarElemento(String elemento){
         NodeLista aux = inicio;
         for (int i = 0; i < size(); i++) {
             if(aux.arvore.elemento.equals(elemento)){
                 return true;
             }
             aux = aux.proximo;
         }
         return false;
     }
 }

NodeTree Class

public class NodeTree {
    String elemento;
    Lista filhos;

    public NodeTree(String elemento){
        this.elemento = elemento;
        filhos = new Lista();
    }

}

NodeList class

public class NodeLista {
    NodeTree arvore;
    NodeLista proximo;
    NodeLista anterior;

    public NodeLista(NodeTree arvore){
        proximo = null;
        anterior = null;
        this.arvore = arvore;
    }

}

Main

public class MainTree {


    public static void main(String[] args) {
        Lista lista = new Lista();
        System.out.println(lista.size());
        NodeTree arvore = new NodeTree("1");
        lista.add(arvore);
        NodeTree arvore2 = new NodeTree("2");
        lista.add(arvore2);
        arvore2.elemento = "3";
        System.out.println(lista.inicio.arvore.elemento);
        System.out.println(lista.inicio.proximo.arvore.elemento);
        System.out.println(lista.size());
    }

}

Console

0 1 3 2

Desired output

0 1 2 2

    
asked by anonymous 24.05.2014 / 21:32

1 answer

1

As you are generating some problems, I will put the excerpt from the (edited) comment that possibly helped to solve the problem:

I think if it can be complemented, better for everyone.

  

When you did: tree2.element="3", you changed the attribute   element of the node that was already within the list, not just the node   external. The list saves references to objects that are in the Heap   (logo: lista.inicio == tree). In Java, if you pass an object as   parameter, is always the reference of the object and not the value, other than C that there is the possibility of passing by parameter and by value.

Read Recommendation: What they are and where are the "stack" and "heap"

    
29.05.2014 / 16:21