So I want to write a programm, that can manage workouts.I have a class "Excersise" and I will create many Objects from this class. Im wondering how I should handle all these Objects. Till now I had an ArrayList with all Excersises in it but when I want to add an Excersise to a workout, I would have to do it like this: workout.add(excerises.get(index))
meaning I would have to know the index for every excerises. Also Im wondering how good the performany of ArrayLists are. Should I consider to use another data structure?
ArrayList for handeling many Objects?
89 views Asked by user9216568 At
1
If you have constant pool of Excersises, and you only need to access them by index, then you can use array instead:
ArrayList is designed to have fast access to element by index, and it is using array underneath, but it has range check inside it's get method. On the other hand ArrayList provide a bunch of useful methods, in case if you need any of them. The other thing is that if you would like to hold a lot of objects in it it might be wise to create it with some capacity, which you expect, knowing of how many objects you will add to this ArrayList, for example if you expect 1000 objects then you can do:
In that way you'll omit recreating of arrays inside ArrayList.add method when array inside ArrayList will not be able to store provided value (HashMap also has constructor
HashMap(int initialCapacity)
).If you would like to access to excersise for example by it's symbolic name, let's say "push ups", then you can use HashMap: