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 7916453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T14:48:50+00:00 2026-06-03T14:48:50+00:00

I’m writing a class that have a method of removing an object from other

  • 0

I’m writing a class that have a method of removing an object from other class. But it just worked inproperly, the output is not correct. Please help me through, and is there any better solution for this, I think my solution is quite cumbersome. Here is my code:

public List<Task> getTaskDue(){        
    List<Task> temp = this.taskCollection;
    for (int unit = 0; unit < this.unitCollection.size(); unit++){            
        for (int j = 0; j < this.unitCollection.get(unit).getAssessmentCollection().size(); j++){
            for (int i = 0; i < temp.size(); i++){
                if (temp.get(i).getDueDate().compareTo(this.unitCollection.get(unit).getAssessmentCollection().get(j).getDueDate()) > 0)
                    temp.remove(i);
            }
        }  
    }
    return temp;
}

Updated: I have Diary class that has list of Task class and Assessment class that hold due date attribute. I want to create a method that return a new list which have a list of over due task by comparing the task from diary class with the due date attribute from assessment class. The program compile successfully but the result is not correct if I want to test the list return no task item since no task is over due.

  • 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-03T14:48:52+00:00Added an answer on June 3, 2026 at 2:48 pm

    It seems like “removing” elements from the list isn’t your ultimate problem.

    You said you want your method to return a new list that contains elements from taskCollection based on some criteria. At the same time, I don’t think you want to destroy or change taskCollection in any way.

    So instead of creating temp as a reference to taskCollection, have it be a new ArrayList<Task>() instead. Then add tasks to temp (the new list) that you want to ultimately return from your method.

    I am going to leave my adivce at that, because your code sample, in isolation, has a lot of unknowns that prohibit me from making any educated guesses on what you really need it to do.

    Also, there are too many for loops! (I’m mostly kidding, but seriously…)


    With more information from the comments below, I’ve modified your code to implement what I am suggesting. In order to add the items to temp (instead of remove them) I had to change your if statement from > 0 to <= 0. Also, instead of iterating over taskCollection in the inner-most loop, you should get tha tasks from the current assessment and iterate over those.

    public List<Task> getTaskDue(){     
        List<Task> temp = new ArrayList<Task>();
    
        for(int u = 0; u < unitCollection.size(); u++){
            Unit unit = unitCollection.get(u);
    
            for (int a = 0; a < unit.getAssessmentCollection().size(); a++){
                AssessmentItem assessment = unit.getAssessmentCollection().get(a);
    
                for (int t = 0; t < assessment.getTasks().size(); t++){
                    Task task = assessment.getTasks().get(t);
    
                    if (task.getDueDate().compareTo(assessment.getDueDate()) <= 0){ 
                        temp.add(task);
                    }   
                }   
            }   
        }   
        return temp;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but

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.