Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8662243
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:41:14+00:00 2026-06-12T16:41:14+00:00

I need help in getting an element of an ArrayList of one class and

  • 0

I need help in getting an element of an ArrayList of one class and using it in another.

The task is basically allocating task entered in Job class which is placed in an arraylist in JobQueue class. The issue im having at the moment is that i have a duration as integers for each task within Job class and im trying to get a total duration utilised in JobQueue class if there is more than 1 task enetered.

Task:- get totalDuration of jobs in Job class to be used inside JobQueue class.

Here is what i have so far.

//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.
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T16:41:16+00:00Added an answer on June 12, 2026 at 4:41 pm

    This would be your method, not much to it:

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

    You also have a totalDuration property that could be seen as a cached total duration value. This should be introduced only as a necessary evil, an optimization due to a noticed performance problem. If that is not your case, remove the property. If you really do need it, given your current API (jobs are only added), you could maintain the total value by changing your addJob method:

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

    And then your required method to get the total duration would be just

    int totalDuration() { return totalDuration; }
    

    However, as your code’s complexity grows, it usually becomes increasingly difficult to mantain the total duration invariant and there’s a breaking point where you must be very clear that you really need its speed advantage.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi need help getting repeated value from give array,below is the code which im
I need some help in getting this right, problem Write a function which takes
I'm getting nowhere with this. I have an element, which is using a :before
EDIT: I believe I need help getting the selected element in the list I
Need help getting Ember-Data working with Zend Rest. At first, I'm familiar with Zend
I need help getting CommonJS working on Java 7 and Rhino 1.7R3. Rhino 1.7R3
I need help getting VIM to correctly indent javascript inside script tags. This is
I need help getting git extensions to run with msysgit. I have had bad
I need help getting a Grease Monkey with JQuery Script to run on a
I need help getting two pieces of data, and comparing them. What I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.