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

  • Home
  • SEARCH
  • 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 9283155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T18:33:29+00:00 2026-06-18T18:33:29+00:00

I have a method that takes a List and another class object as its

  • 0

I have a method that takes a List and another class object as its arguments. I want to iterate through the list based on the class object that I am passing into the method. The class has three different constructors. One of them takes Date objects, the other takes strings, and the third takes ints. In my method, I would like to do different things depending on which constructor is being used for the class object being used. Is there a way to do this? Is there a way I can do an if else statement that checks for the type of class object (based on constructor) being used?

For CodeWarrior: Here are constructor examples:

    public DateRange(Date date1, Date date2){

}

public DateRange(String string1, String string2){

}

public DateRange(int month, int year){

}

Then say I have a method like this:

public static List<Schedule> getSchedule(List<Schedule> schedules, DateRange dateRange) {
    List<Schedule> schedules = new ArrayList<Schedule>();
    for (Schedule scheduleTime : schedules){
        if

I would like to set up the method to design the if statements to somehow check which type of the DateRange had been used, and do different things based on that.

  • 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-18T18:33:31+00:00Added an answer on June 18, 2026 at 6:33 pm

    I think this is what you are looking for.

    Modify your DateRange class so it can tell you how it was created:

    public class DateRange {
        private final Object obj1;
        private final Object obj2;
        public DateRange(Date date1, Date date2) {
            obj1 = date1;
            obj2 = date2;
        }
        public DateRange(String string1, String string2) {
            obj1 = string1;
            obj2 = string2;
        }
        public DateRange(int month, int year) {
            obj1 = Integer.valueOf(month);
            obj2 = Integer.valueOf(year);
        }
        public boolean isDate() { return obj1 instanceof Date; }
        public boolean isString() { return obj1 instanceof String; }
        public boolean isInt() { return obj1 instanceof Integer; }
    }
    

    Here is the calling/checking syntax:

    public static List<Schedule> getSchedule(List<Schedule> schedules, DateRange dateRange) {
        for (Schedule scheduleTime : schedules) {
            if(dateRange.isDate()) {
                // Do something based on the Date constructor.
            } else if(dateRange.isString()) {
                // Do something based on the String constructor.
            } else if(dateRange.isInt()) {
                // Do something based on the int constructor.
            }
        }
        return schedules;
    }
    

    Another option requiring further modification of your DateRange class would be to use inheritance to make a family of DateRange classes based on the constructor type. The base DateRange class would have an abstract method akin to doSomething() which would be provided by each of the inherited DateRangeDate, DateRangeString, and DateRangeInt classes. Then, you might not even have to process through the if tree in your iteration.

    I hope this helps!

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

Sidebar

Related Questions

I have a method that takes a list of entities ( Class es) and
Say I want to have a method that takes any kind of number, is
I have a static method that takes a parameter and returns a class. the
I have a method that takes a java.util.Properties object as a constructor argument. It's
I have a method that takes an array of queries, and I need to
So I have a method that takes a tag and wraps the selected text
I am trying to have a method that takes in a username and will
All, I have a method that takes a date (YYYY-MM-DD H:M:S) from the database
I'm wondering how to go about testing this. I have a method that takes
I have a search method that takes in a user-entered string, splits it at

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.