Hello. I'm trying to make an ArayList with data that includes when the user logged in and logged out and save everything in a file like the date and the total time and so on. The problem is that I get stuck on a simple issue.
I have this class:
public class DayTimeFrame { int id; public DayTimeFrame() { FromHour = new ArrayList<Integer>(); ToHour= new ArrayList<Integer>(); FromMinute= new ArrayList<Integer>(); ToMinute= new ArrayList<Integer>(); } public void AddFromHour(int hour) { FromHour.add(hour); } public void AddFromMinute(int minute) { FromMinute.add(minute); } public void AddToHour(int hour) { ToHour.add(hour); } public void AddToMinute(int minute) { ToMinute.add(minute); } public ArrayList<Integer> FromHour; public ArrayList<Integer> ToHour; public ArrayList<Integer> FromMinute; public ArrayList<Integer> ToMinute;}
And with that structure I want to make another Array that contains the element for all days in a month. So day 1 is a DayTimeFrame object that can contain more Integer elements like FromHour, To Hour and so on. If the user loggs 100 times in day 5 the element in the ArrayList that us the 5th one contains the DayTimeFrame that contaons 100 FromHour and ToHour elements.
The thing is that when I want to modify the elements for conviencence I first fill the
ArrayList<DayTimeFrame> dayTimeFrame = new ArrayList<DayTimeFrame>(); for(int i=0;i<=31;i++) { dayTimeFrame.add(emptyFrame); }
[code]
And when the user updates a day I just modify like this:
[code]dayTimeFrame.get(1).AddFromMinute(11);dayTimeFrame.get(5).AddFromMinute(22);dayTimeFrame.get(7).AddFromMinute(33);dayTimeFrame.get(0).id=8;
The problem is that even tho I specifically select only 1,5 and 7 and 0 index elements ALL THE ELEMENTS in the array change. Its like the objects share the same ArrayList<Integer>. I dont understand why. I come from C++ and I know if I declare an object inside another one it gets local to that object. Why the get function modifies all the elements ? Or is the problem with the members of the class and its another way to declare them?