首页 > 解决方案 > 如何根据特定条件从列表中删除对象(java)?

问题描述

我试图找到一种方法来根据条件从列表中删除对象,该对象是具有年龄属性的患者,我想从列表中删除每个年龄超过 25 岁的患者,代码由 4根据某些信息分类的列表是我的代码:

   public static void main(String[] args) {
        // TODO code application logic here
        Scanner in = new Scanner(System.in);
        Scanner kb = new Scanner(System.in);
        boolean flag = true;
   
    System.out.print("Enter number of patients: ");
    int n = in.nextInt();
    
    AList patients  = new AList(n); //All Patients
    AList Apatients = new AList(); //category "A" Patients
    AList Bpatients = new AList(); //category "B" Patients
    AList Cpatients = new AList(); //category "C" Patients
   
    //Add Patients to the list:
    for(int i =0; i < n ; i++){
        Patients p = new Patients();
        System.out.print("Enter Patient name: ");
        p.name = kb.nextLine();
        
        System.out.print("Enter Patient ID: ");
        p.id = in.nextInt();

        System.out.print("Enter Patient Age: ");
        p.age = in.nextInt();            
        
        flag = true;
        while(flag){
            System.out.print("Enter Patient Category: ");
            p.category = kb.nextLine();
            System.out.println("\n\n");
            if(p.category.equals("a") || p.category.equals("A")){
                Apatients.add(p);
                flag = false;
            }
            else if(p.category.equals("b") || p.category.equals("B")){
                Bpatients.add(p);
                flag = false;
            }
            else if(p.category.equals("c") || p.category.equals("C")){
                Cpatients.add(p);
                flag = false;
                
            }
            else{
               System.out.println("Wrong Entry...! Try Again");
            }
        }
        patients.add(p);
    }    
    //Display all patients:
    System.out.println("All Patients information : ");
    patients.display();
    
    System.out.println("Category A Patients : ");
    Apatients.display();
    
    System.out.println("Category B Patients : ");
    Bpatients.display();
    
    System.out.println("Category C Patients : ");
    Cpatients.display();
    
    //From the list for A category remove each patient with age > 25 year:
    for(int i=0; i < Apatients.getLength();){
        // here is my problem how to iterate the list and check for any patient age > 25 to remove it? 
    }
}

标签: java

解决方案


这段代码中有很多很多错误。

2 个扫描仪

您应该只有一个 Scanner 对象。为什么你同时拥有kband in?删除一个。

混合nextLinenextAnythingElse

你不能混合这些。通常的做法是永远不要使用nextLine(); 要阅读一行文本,只需使用next(). 这确实需要您更新扫描仪的分隔符;在制作扫描仪对象后立即调用in.useDelimiter("\r?\n");它。

AList 不是一个东西

“AList”可能是什么?它不在核心 java 中,听起来这里不需要它;只是ArrayList<Patient>使用

用复数名称命名一个类。

显然,单个Patients实例代表单个患者,因此,您应该将其命名为Patient,因为您所拥有的显然会让您感到困惑。

你打破常规。

thisIsAVariableName, ThisIsATypeName, THIS_IS_A_CONSTANT. 应该是aPatients,不是APatients。这使您的代码难以阅读,并且鉴于您使用的是社区资源(堆栈溢出),这并不好。

你的实际问题

鉴于 AList 不是一个东西并且您没有粘贴它,因此 SO 上的任何人都不可能回答这个问题。更新问题并包含 的 API AList,或停止使用它(没有理由使用它,除非因为这是家庭作业而强制要求,在这种情况下,您应该向您的老师寻求帮助;他们得到报酬,并且是那些应该支持AList你正在使用的这个类的人)。如果您一直在使用 ArrayList,那么有很多方法可以做到这一点。但首先有一个问题:如果您通过“索引”循环遍历每个项目,那么删除一个元素会将所有索引向下移动一个,这会使数学变得复杂。摆脱这种困境的一种极其简单的方法是从后往前循环遍历列表,因为这样索引的移动不会产生影响。

选项 #1:迭代器

var it = aPatients.iterator();
while (it.hasNext()) {
    Patient p = it.next();
    if (p.getAge() > 25) it.remove();
}

选项#2:向后循环

for (int i = aPatients.size() - 1; i >= 0; i--) {
    if (aPatients.get(i).getAge() > 25) aPatients.remove(i);
}

选项#3:removeIf

aPatients.removeIf(pat -> pat.getAge() > 25);

推荐阅读