gpt4 book ai didi

java - 使用循环显示特定的 arraylist 对象

转载 作者:行者123 更新时间:2023-12-01 06:07:49 25 4
gpt4 key购买 nike

所以我需要显示数组列表中整数值超过 7 的某些元素。到目前为止,我已经写了这个,但我正在努力使其工作。有人可以帮忙吗?

public Diary()
{
jobname = new ArrayList<Job>();
}


public void priority ()
{
int index = 0;
while (index < jobname.size ()) {
Job jobName = jobname.get (index);
index ++;
//if (jobname.get (priority)) {
// System.out.println (jobname);
//}
}
}

这是日记引用的 Job 类。

public Job(String job, int priorityNum, String day) 
{
if (priority < 0 || priority > 10) {
priority = 0;
}
else {
priority = priorityNum;
}
jobname = job;
dueDay = day;
}

public boolean isUrgent()
{
return priority > 7;
}


public String toString ()
{
String s = "";
s += "Job = " + jobname + " \n";
s += "Day Due = " + dueDay + " \n";
s += "Priority = " + priority + " \n";

return s;
}

最佳答案

您需要将代码更改为以下内容:

public void priority ()
{
foreach(Job job : jobname){
if (job.getPriority() > 7) {
System.out.println (job);
}
}
}

其次,在 Job 类中添加 getPriority(),该类将返回 priority 的值:

public String getPriority(){
return this.priority;
}

关于java - 使用循环显示特定的 arraylist 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40936682/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com