3

我需要帮助来获取一个类的 ArrayList 的元素并在另一个类中使用它。

该任务基本上是分配在 Job 类中输入的任务,该任务放置在 JobQueue 类的 arraylist 中。我目前遇到的问题是,对于 Job 类中的每个任务,我都有一个整数作为整数,如果输入的任务超过 1 个,我试图获取 JobQueue 类中使用的总持续时间。

任务:- 获取 Job 类中要在 JobQueue 类中使用的作业的总持续时间。

这是我到目前为止所拥有的。

//first class that contains jobs created.
public class Job
{  
//fields  
private String jobName;
private int jobDuration;
private static final int DEFAULT_DURATION = 0;

public Job(String task, int duration)
{
    //constructor used to create the job name and how long it will take to do one job (duration).
    jobName = task;
    if (duration > DEFAULT_DURATION)
    {
        jobDuration = duration;
    }
    else
    {
        jobDuration = DEFAULT_DURATION;            
    }
}
public String getName()
{
    return jobName;
}

public int getDuration()
{
    return jobDuration;
}
}

// 2nd class used to enter every job created into an Arraylist called myJobs.
public class JobQueue
{
private ArrayList <Job> myJobs;
private int totalDuration;
private static final int DEFAULT_NUM = 0;

public JobQueue()
{
    myJobs = new ArrayList<Job>();
    totalDuration = DEFAULT_NUM;
}
public ArrayList<Job> getPendingJobs()
{
    return myJobs;
}
public void addJob(Job job)
{
    myJobs.add(job);
}
}
//here i need a method that gets the total duration of jobs in the arraylist and returns total as an integer. Please help.
4

9 回答 9

2

循环获取总持续时间

public int getTotalDuration() {
    int total = 0;
    for(Job job : myJobs) {
        total += job.getDuration();
    }
    return total;
}
于 2012-10-11T13:42:34.020 回答
1

除非我误解了目的,否则我认为您不应该拥有totalDuration该课程的财产JobQueue。相反,创建一个方法,称为类似的方法,getTotalDuration并让它遍历属性Job中的所有 s myJob,将每个单独Job的 s相加jobDuration

public int getTotalDuration() {
    int totalDuration = 0;
    for (Job job : this.myJobs) {
        totalDuration += job.getDuration();
    }
    return totalDuration;
}
于 2012-10-11T13:42:09.427 回答
1

这将是你的方法,对它来说并不多:

int totalDuration() {
  int totalDuration = 0;
  for (Job j : myJobs) totalDuration += j.getDuration();
  return totalDuration;
}

您还有一个totalDuration可以被视为缓存总持续时间值的属性。这应该仅作为必要的邪恶引入,即由于注意到性能问题而进行的优化。如果这不是您的情况,请删除该属性。如果您确实需要它,给定您当前的 API(仅添加作业),您可以通过更改addJob方法来保持总价值:

public void addJob(Job job)
{
    myJobs.add(job);
    totalDuration += job.getDuration();
}

然后您获得总持续时间所需的方法就是

int totalDuration() { return totalDuration; }

但是,随着代码复杂性的增加,保持总持续时间不变通常变得越来越困难,并且有一个断点,您必须非常清楚您确实需要它的速度优势。

于 2012-10-11T13:42:44.450 回答
1

如果您想要作业列表中所有作业的总持续时间,只需遍历 ArrayList 并总结每个单独任务的持续时间:

public int getDuration()
{
    int totalDuration = 0;
    for (Job job : myJobs)
    {
        totalDuration += job.getDuration();
    }
    return totalDuration;
}

请注意,这是解决问题的一种简单方法,因为还有其他考虑因素可能对您很重要。例如,如果作业名称是唯一的,您可能希望在遇到重复的作业名称时执行特殊行为。例如,如果您想忽略重复项,您可以保留您已经遇到的名称的 HashSet。在任何情况下,您都需要确定此场景所需的行为。

于 2012-10-11T13:45:22.647 回答
1
public int getDuration(){

    int totalDuration=0;

    for(Iterator<Job> it =myJobs.iterator(); it.hasNext(); ){
        Job job = it.next();
        totalDuration+=job.getDuration();
    }

    return totalDuration;

}
于 2012-10-11T13:50:11.553 回答
0
for (Job job : getPendingJobs())
{
    totalDuration += job.getDuration();
}

就这样

于 2012-10-11T13:43:04.480 回答
0

遍历 arraylist 并总结每个作业实例的 jobDuration。

 for(Job j: jobList) {
      //sum up all the durations here
 }
于 2012-10-11T13:44:11.110 回答
0

这可能会有所帮助。

//first class that contains jobs created.
public class Job {
    //fields  
    private String jobName;
    private int jobDuration;
    private static final int DEFAULT_DURATION = 0;
    JobQueue queue = new JobQueue(); // Intialize the object.. Now queue.myJobs = a new array list of jobs.

    public Job(String task, int duration) {
        //constructor used to create the job name and how long it will take to do one job (duration).
        jobName = task;
        if (duration > DEFAULT_DURATION)
        {
            jobDuration = duration;
        }
        else
        {
            jobDuration = DEFAULT_DURATION;            
        }

        queue.addJob(this);

    }
    public String getName() {
        return jobName;
    }

    public int getDuration() {
        return jobDuration;
    }
}

// 2nd class used to enter every job created into an Arraylist called myJobs.
public class JobQueue {
    private ArrayList <Job> myJobs;
    private int totalDuration = 0;
    private static final int DEFAULT_NUM = 0;

    public JobQueue() {
        myJobs = new ArrayList<Job>();
        totalDuration = DEFAULT_NUM;
    }
    public ArrayList<Job> getPendingJobs() {
        return myJobs;
    }
    public void addJob(Job job) {
        myJobs.add(job);
        totalDuration += job.getDuration(); // It would be easier instead of looping through it, just to add to the total duration when the job is added to the queue.
    }

    public void getTotalDuration() {
        return totalDuration;
    }
}
于 2012-10-11T13:46:24.600 回答
0

改变你add的方法

public void addJob(Job job)
{
    myJobs.add(job);
    totalDuration += job.getDuration();
}

现在您有getTotalDuration()方法返回totalDuration.您还需要减去已删除的工作。

public void getTotalDuration() {
    return totalDuration;
}
于 2012-10-11T13:49:34.877 回答