java - 如何根据特定条件从列表中删除对象(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?
}
}
解决方案
这段代码中有很多很多错误。
2 个扫描仪
您应该只有一个 Scanner 对象。为什么你同时拥有kb
and in
?删除一个。
混合nextLine
和nextAnythingElse
你不能混合这些。通常的做法是永远不要使用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);
推荐阅读
- google-api - 使用 google ad manager api 删除订单项
- python - 如何通过 Google 电子表格进行搜索
- c - 在 C 中正确使用#if 指令?
- postgresql - pg_upgrade 期间的 postgres 超级用户问题
- java - 我可以在我的 Java 应用程序中安装嵌入 Neo4j 的图形数据科学 (GDS) 库吗?
- r - If else 语句的值是 R 中连续字符的一部分
- xml - 将“固定”键添加到引用的属性不起作用
- scp - 不再支持 SSH 协议 v.1
- python - 为什么我的 while 循环不会结束我的石头、纸、剪刀游戏?
- python - Python 请求包含有值的参数和没有值的参数