Insert object from a Linkedlist to a Linkedlist from another class

Asked

Viewed 158 times

1

I have two classes in my project: Cars and Cars.

At first, I have a Linkedlist stated as follows:

private LinkedList<Carro> carros = new LinkedList<Carro>();

Then I have methods of insertion, deletion and listing. However, I also needed to implement a method to receive Car type objects from the class I created to implement the methods referring to cars with the idle situation. This method simply takes an object inserts it at the end of the Linkedlist of my Cars class.

public void alocaCarros(Carro carro) {
        this.carros.add(carro);
    } 

In my class Carrosinativos I have a Linkedlist to store the cars in inactivity:

private LinkedList<Carro> carrosInativos = new LinkedList<Carro>();

To carry out the inclusion of these inactive cars in the list of active cars when needed, I invoked the Cars class within the Carrosinativos class.

Carros carrosAtivos = new Carros();

Within this class, I have the method responsible for passing the objects entered in the inactive car queue to the allocation method:

public void InsereCarros(){
carrosAtivos.alocaCarros(carrosInativos.getFirst());
                carrosInativos.remove();
}

Here is the problem: in the tests, both my list of active cars and the list of inactive cars are receiving objects normally; however, when invoking the method, the insertion of the object passed is not performed.

NOTE: When inserting a System.out.println in the class allocateCarros using the received object as parameter, the object is listed normally in the output.

1 answer

1

Good afternoon, I was a little confused about the explanation but I tried to spin on what you asked. It worked right here. Take a look at my classes and see if you can help with that. Ahhh I would do differently, create a Car class and leave a Boolean attribute for active. Think about it later, I think it would look better.

import java.util.LinkedList;

public class CarrosInativos {

private LinkedList<Carro> carrosInativos = new LinkedList<Carro>();

private Carros carrosAtivos = new Carros();

public void InsereCarros() {
    carrosAtivos.alocaCarros(carrosInativos.getFirst());
    carrosInativos.remove();
}

// Adicionei para poder inserir na classe de testes.
public void add(Carro carro) {
    this.carrosInativos.add(carro);
}

// Adicionei para poder recuperar na classe de testes
public LinkedList<Carro> getCarrosInativos() {
    return carrosInativos;
}

public Carros getCarrosAtivos() {
    return this.carrosAtivos;
}

 }

import java.util.LinkedList;

public class Carros {

private LinkedList<Carro> carros = new LinkedList<Carro>();

public void alocaCarros(Carro carro) {
    this.carros.add(carro);
}

// Adicionei para poder recuperar na classe de testes.
public LinkedList<Carro> getCarros() {
    return carros;
} 

   }


public class TestaCarro {

public static void main(String[] args) {
    Carro carro = new Carro("amarelo");
    Carro carro2 = new Carro("azul");
    Carro carro3 = new Carro("verde");

    CarrosInativos carrosInativos = new CarrosInativos();

    carrosInativos.add(carro);
    carrosInativos.add(carro2);
    carrosInativos.add(carro3);

    for ( Carro c : carrosInativos.getCarrosInativos()) {
        System.out.println(c);          
    }

    carrosInativos.InsereCarros();
    System.out.println("--------------");
    for ( Carro c : carrosInativos.getCarrosInativos()) {
        System.out.println(c);          
    }

    System.out.println("----");
    for ( Carro c : carrosInativos.getCarrosAtivos().getCarros()) {
        System.out.println(c);
    }

}
  }

Browser other questions tagged

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