Reputation:
I have used ArrayLists in my application, so I do not want to move to HashMaps as I have seen a few answers using this but did not work in my case.
I am creating my final high school project which is a study time table scheduler. I have managed to sort the ArrayList by date of the exam, but now I am trying to analyze the time table to rate it and compare the ratings with other generated timetables to give the user the best timetable (with the highest value).
I have gotten the timetable to output what they will be doing on each day ie: Afrikaans, IT, LO, Maths, Afrikaans, Afrikaans, etc.
But it seems that it duplicates on the last one for a long time even if the two last exams are within 2 days or very close together, the last one will be more dominant in the entire list. Like this:
Exam Dates:
LO = 2 Sep
Maths = 5 Sep
IT = 9 Sep
Afrikaans = 10 Sep
Results:
LO
IT
Afrikaans
Maths
IT
Afrikaans
Afrikaans
Afrikaans
Afrikaans
As you can see, the result is much more weighted towards the last given subject.
Now I am trying to compare and group the data so I can see how many days were allocated to each subject, so using the above example I need it to look like this:
LO = 1
Maths = 1
IT = 2
Afrikaans = 5
So far I have this:
ArrayList<Day> days = new ArrayList<Day>();
For(int i = 0; i < timeTable.size(); i++) {
// need to group the days here
if(days.contains(timeTable.get(i))) {
days.get(i).incrementNumberOfDays();
} else {
days.add(timeTable.get(i));
}
}
But that is not grouping the data and counting the duplicates. Please help, my project is due in a week and I have spent two months on it. I am almost finished and am stuck here.
Thanks in advance!
Upvotes: 0
Views: 2051
Reputation: 588
It's necessary to mention that in your timeTable
, assuming it contains two same Day
- Afrikaans - 1
, when you see they are the same, the contains
function doesn't think so. Although they have the same value for each property, but not the hashcode.
You need to override the function of equals()
to make sure that when comparing two Day
, if they have the same value, they are regarded as one object.
Sorry for my poor English, hope you can get it.
Upvotes: 0
Reputation: 2596
You can use java.util.Collections.frequency
:
int count = Collections.frequency(timeTable, value);
Edit: answer to your last question in comment:
Surely there is a quick simple way to say this value occurred 3 times in the arraylist?
Upvotes: 1