Reputation: 642
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?
Upvotes: 3
Views: 168
Reputation: 477
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.
Upvotes: 4