简体   繁体   中英

Elements are not removed from list

i am newbee in java and trying to remove elements from list array. Have been tried many variants, but always get input error and nothing been deleted from list. Have tried take make condition like: if (list.contains(tch.getSurname()) or something like this, always get error that input error. Hope, you will help me to resolve this problem.

package com.company;

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public class Main {

    static Main main = new Main();
    /*public static Teachers tch = new Teachers(surname, name);*/
    public static List<Teachers> list = new ArrayList<Teachers>();

    public static void io(){
        Scanner sc = new Scanner(System.in);
        String surname = "";
        String name = "";
        Teachers tch = new Teachers(surname, name);
        for (int i=0; i<2; i++) {
            surname = sc.nextLine();
            name = sc.nextLine();
            tch = new Teachers(surname, name);
            list.add(tch);
        }
        for(Teachers nstr : list) {
            System.out.println(nstr.toString());
        }

        for(Teachers t : list) {
            String input = sc.nextLine();
            if (input == tch.getSurname()) {
                list.remove(input);
            } else {
                System.out.println("Wrong input");
            }

        }

        for(Teachers nstr : list) {
            System.out.println(nstr.toString());
        }
    }

    public static void main(String[] args) {
        main.io();


    }
}

UPDATE: So i'm tried to use iterator, i've added:

for (Iterator<Teachers> it = list.iterator(); it.hasNext();){
            Teachers t = it.next();
            if (t.equals(tch.getSurname())){
                it.remove();
            }
        }

and delete:

for(Teachers t : list) {
            String input = sc.nextLine();
            if (input.equals(tch.getSurname())) {
                list.remove(tch);
            } else {
                System.out.println("Wrong input");
            }
        }

But it also didn't help me to delete element from list array, it just duplicate my input for list.

Your error is if(input == tch.getSurname() . This compares the exact reference to see if it is the same. Instead, use if(input.equals(tch.getSurname())) to check the contents. Also change the reference in that loop from tch to t . You are not using the current element in the list rather the one you created to add to the list. Finally change list.remove(input) to list.remove(t) . This way the actual element is being removed, no just trying to remove the surname string.

Change your for loop to

Iterator<Teachers> i = list.iterator();
while(i.hasNext()){
     Teachers t = i.next();
      ...
}

Then to remove it just use i.remove()

Now loop works perfectly.

for(Teachers t : list) {
                String input = sc.nextLine();
                if (input.equals(t.getSurname())) {
                    list.remove(t);
                } else {
                    System.out.println("Wrong input");
                }
            }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM