如何从列表中获取元素并将其删除?

时间:2013-11-30 17:23:55

标签: java

我需要创建一个删除元素的方法,该元素与从键盘输入的ID具有相同的ID。 我试过在网上看,但我还是不知道如何获取元素。

//

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;
    }

我在课程中调用方法RemovePet:

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());
        }

我认为我的代码不是由输入的ID运行在列表上不能直接比较,但我不能使用maXoa.equals(listPet.ID)。

1 个答案:

答案 0 :(得分:1)

喜欢这个

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