3

I am currently doing an app that will have many exercises (for working out) which each contain 5 different strings. They are linked to workouts that are basically arrays of exercises (but more than one workout can have the same exercise and I don't want to waste memory), and then workouts are displayed in different categories (weight loss, body building, etc..).

If I was doing this in C (which is the language I usually program in) I would have the exercises to be an array of a structure called exercise, then the workout would be linked lists with each link being a pointer to the exercise, with an int for reps and an int for sets. And the workouts would be organized in an array of pointer pointing to the first element of the linked list (so a workout can be in multiple categories). Is there a way to implement something like that in java?

1 Answer 1

4

Working backwards from your method in c, why not have something like:

class Exercise {
 private String exName;
 private int reps;
 private int sets;

And making a constructor:

 public Exercise(String newName, int newReps, int newSets) {
  exName = newName;
  reps = newReps;
  sets = newSeats;

That's really the only part that is different from c.

Here, all you're doing is making an Exercise object which contains the necessary information, reps and sets.

The constructor makes a new instance of the object.

Then you can just use a regular arraylist for the list of exercises and a list of workouts.

So you end up with something that you can visualize like:

[[Exercise1, Exercise2, ...][Exercise1, Exercise2, ....]...]

So the outermost list is your arraylist of workouts, the inner list is your arraylist of exercises for each workout, and each exercise is a structure that contains an (optional) name, along with its list of reps and sets.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for your response, is there a way to make the workout a class that has an array of Exercises? because the sets and reps have to be in the workout because it might change between workouts
In my solution, Workout would be a class. Each instance of workout would make an arrayList of exercises, with each exercise being what I provided above.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.