Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

530
Visualizações
How to remove an object from a linked list in java

I have a linkedlist called seatList made up of Seat objects set up like so...

public class Seat {

    int row, col;
    char type;

    Seat(int row, int col, char type){
        this.row = row;
        this.col = col;
        this.type = type;
    }

    String printSeat(){
        return "(" + row + ", " + col + ", " + type + ")";
    }

}

When i try to remove the seats with the same properties by creating a new seat object and storing it in a seperate linked list called collections, they do not get removed from the Linked List.

LinkedList<Seat> collection = new LinkedList();

    for (int q : indexToRemove){
        collection.add(new Seat(seatList.get(q).row, seatList.get(q).col, seatList.get(q).type));
    }

    for (Seat s : collection){
        if (seatList.contains(s)){
            println("true");
            seatList.remove(s);
        }

    }

    if (orders.get(indexes.get(index)).seatList.isEmpty()){
        orders.remove((int)indexes.get(index));
    }

Reason it is different: linked question talks about altering the list you are iterating as stated by csm_dev

over 4 years ago · Santiago Trujillo
3 Respostas
Responde à pergunta

0

To remove object from list you have to use Iterator and call remove() on iterator an example :

    Iterator<Seat> collection = seatList.iterator();
    while (collection.hasNext()) 
    {
       Seat element = collection.next();

       //some condition on your object "element"

        collection.remove();
      }

and don't foget to define hashCode/equals methodes logic to compare your Seat objects

over 4 years ago · Santiago Trujillo Relatório

0

You have to use Iterator for this:

Scanner scr= new Scanner(System.in);
System.out.print("Enter Seat Row number to delete: ");
int row = scr.nextInt();

for (Iterator<Seat> iter = list.iterator(); iter.hasNext();) {

    Seat data = iter.next();

    if (data.getRow() == row) {
        iter.remove();
    }
}

You have to add setters and getters in your been class.

over 4 years ago · Santiago Trujillo Relatório

0

LinkedList & other list implementations uses equals() method to check if the object is present in the list. Since you don't have equals() method implemented, java will use the implementation provided by Object class, which is very crude & not work for you.

You need to implement equals() method & give jvm a way to see if 2 objects of seat are same.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda