taki
taki

Reputation: 75

how to get element from list and remove it?

I need to make a method to remove an element have the same ID as ID entered from the keyboard. I've tried looking online, but I still can not know how to get element.

//

public class PetManament {
    private List<Pet> listPet;

    public PetManament() {
        listPet = new ArrayList<Pet>();
    }
    public void RemovePet(String maXoa) {
        int dem = 0;
        for (int i = 0; i < listPet.size(); i++) {
            for (Pet s : listPet){
                if (maXoa.equals(s.id)) {
                    dem=i;
                    listPet.remove(dem);
                    System.out.println(dem);
                }
                }
        }
    }

//

public class Pet {
    protected String id;
    protected String name;
    protected double weight;
    protected Date date;
    public Pet(){}
    public Pet(String ma, String ten, double trongLuong, Date ngayNhap) {
        this.id = ma;
        this.name = ten;
        this.weight = trongLuong;
        this.date = ngayNhap;   
    }

//

public class Monkey extends Pet {
    private String food;
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyy");
    public Monkey(){}
    public Monkey(String ma, String ten, double trongLuong, Date ngayNhap,
            String loaiTAYT) {
        super(ma, ten, trongLuong, ngayNhap);
        this.food = loaiTAYT;
    }

//

public class Lion extends Pet {
    private double meatED;
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyy");
    public Lion(){}
    public Lion(String ma, String ten, double trongLuong, Date ngayNhap,
            double khoiLuongThit) {
        super(ma, ten, trongLuong, ngayNhap);
        this.meatED = khoiLuongThit;
    }

//

public class Snake extends Pet{
    private double length;
    SimpleDateFormat format = new SimpleDateFormat("dd/MM/yyy");
    public Snake(){}
    public Snake(String ma, String ten, double trongLuong, Date ngayNhap,
            double chieuDai) {
        super(ma, ten, trongLuong, ngayNhap);
        this.length = chieuDai;
    }

I'm call method RemovePet in class program:

public class Program {
    static PetManament list=new PetManament();
    public static void main(String[] args) {
        int stepm=1;
        do{
        System.out.println("");
        System.out.println("(1): Add new");
        System.out.println("(2): Remove");
        System.out.println("(3): edit pet information ");
        System.out.println("(4): Search by id or name ");
        System.out.println("(5): list");
        System.out.println("(6): ");
        System.out.println("(7): ");        
        System.out.println("(8): ");    
        Scanner s = new Scanner(System.in);
        int step=s.nextInt();       
        switch(step){
        case 1:
            AddNew();           
            break;
        case 2:
            Remove();
            break;
        case 3:
            break;
        case 4:
            break;
        case 5:
            PrintL();
            break;
        case 6:
            break;
        case 7:
            break;
        case 8:
            stepm=0;

        }
        }while(stepm==1);
    }
    private static void Remove() {      
        Scanner s = new Scanner(System.in);
        System.out.println("Nhap ma con vat muon xoa:");
        String maXoa=s.next();
        list.RemovePet(maXoa);          

        System.out.println(list.getListThuNuoi());
        }

I think my code is not run by the ID entered is not directly comparable on the list, but I can not use maXoa.equals(listPet.ID).

Upvotes: 1

Views: 71

Answers (1)

Prabhakaran Ramaswamy
Prabhakaran Ramaswamy

Reputation: 26094

do like this

public void RemovePet(String maXoa) {    
     Iterator<Pet> iter=listPet.iterator();
     while(iter.hasNext()){
        Pet p=iter.next()
        if(maXoa.equals(p.id)){
            iter.remove();
        }
    }
} 

Upvotes: 1

Related Questions