-1

I'm familiar with Python, where you can make a list of values:[3, 22, 1, 88] and even makes lists of lists: [[1, 2], ["A", "dog", 33],[1, 2, 3, 4]]

And those can put put into pretty much anything.

In Java, I've created a Object class that holds a bunch of info for an activity, and I'm trying to come up with a way to store the days of the week (Mon, Tues, Wed, etc) that the activity happens on, as well as the dates of the month it can happen on (1st, 23rd, 29th).

Was was thinking I'd store them in an arraylist, then use .contains to see if a particular day is in that object. So if the activity was scheduled for Mondays, Tuesdays, and Fridays, I'd create an arraylist of strings with "Monday", "Tuesday", and "Friday" in it, and put the arraylist into the object.

Or I could do an array with 7 indexes (0 for Monday, 1 for Tuesday...6 for Sunday) and store a Boolean in each index location based on which days are in that activity. Then I could just pull that index to see if the activity is scheduled for that day.

But can I store that array in the object? The object has a string for mActivityName, mActivityLocation, int mNumberOfPeople, and so on. Can I shove the array in as mDaysOfWeek? If so, how?

Thanks so much, Seth

  • Possible duplicate of [Java equivalent of Python List](https://stackoverflow.com/questions/48772017/java-equivalent-of-python-list) – Logan May 28 '18 at 18:27

2 Answers2

0

You can add the ArrayList as an extra attribute for your Object. So if you want a list containing the days of the week where the event is going to be available it would better to have an ArrayList of String. Like this:

public class MyObject {

private ArrayList<String> daysOfweek = new ArrayList<>();

}

You can create the proper getter and setter for this new attribute as well as the contains, add and remove methods. If you want to access the object directly just declare it as public.

Marcos Guimaraes
  • 1,243
  • 4
  • 27
  • 50
0

If you wanted to go for your second approach, it would look something like this:

public class Activity {
   String activityName;
   String activityLocation;
   int numberOfPeople;
   boolean[] schedule;
}

Declaring it like this will leave the array as null until it has been initialized. You could instead initialize it, like this:

public class Activity {
   String activityName;
   String activityLocation;
   int numberOfPeople;
   boolean[] schedule = new boolean[7];
}

The value of schedule would now be [false, false, false, false, false, false, false]. You could update it with a classic setter:

// In the activity class
private void setSchedule(boolean[] schedule) {
   this.schedule = schedule;
}

// In whichever code is creating your activities
Activity activity = new Activity();
...
boolean[] mondaysAndWednesdays = {true, false, true, false, false, false, false};
activity.setSchedule(mondaysAndWednesdays);
...

Alternatively, you could include a convenience method that would schedule an activity for a particular day:

// In the Activity class
private void scheduleForDayX(int x) {
    schedule[x] = true;
}

// In whichever code is creating your activities
Activity activity = new Activity();
...
activity.scheduleForDayX(0);
activity.scheduleForDayX(2);
...

However, this is not the approach I would suggest. I would instead create an enum with the 7 days of the week, and store some collection of those (probably a set) as described in the other answer. Some information on using enums instead of strings: Why use Enums instead of Constants?

Kayla C.
  • 11
  • 4