从一个类的arraylist中获取一个元素,以便在另一个类中使用

时间:2012-10-11 13:38:20

标签: java arraylist

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

该任务基本上是分配在Job类中输入的任务,该任务放在JobQueue类的arraylist中。我现在面临的问题是,我有一个持续时间作为Job类中每个任务的整数,并且我试图获得在JobQueue类中使用的总持续时间,如果有超过1个任务被激活。

任务: - 获取Job类中作业的totalDuration,以便在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.

9 个答案:

答案 0 :(得分:2)

进行循环以获得总持续时间

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

答案 1 :(得分:1)

除非我误解了目的,否则我认为totalDuration类不应该有JobQueue属性。相反,创建一个名为getTotalDuration的方法,让它循环遍历Job属性中的所有myJob,将每个JobjobDuration相加}。

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

答案 2 :(得分: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; }

但是,随着代码复杂性的增加,通常会越来越难以保持总持续时间不变,并且有一个突破点,你必须非常清楚你真的需要它的速度优势。

答案 3 :(得分:1)

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

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

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

答案 4 :(得分:1)

public int getDuration(){

    int totalDuration=0;

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

    return totalDuration;

}

答案 5 :(得分:0)

for (Job job : getPendingJobs())
{
    totalDuration += job.getDuration();
}

这就是全部

答案 6 :(得分:0)

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

 for(Job j: jobList) {
      //sum up all the durations here
 }

答案 7 :(得分: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;
    }
}

答案 8 :(得分:0)

更改add方法

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

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

public void getTotalDuration() {
    return totalDuration;
}
相关问题