1 Attachment(s)
Counting objects in ArrayList
Hi
I got a problem
I want to show "statistics" of how many times a certain object is in my arraylist, gotten from my database, to send to my jsp page
my code isn't the best but here it is
Code:
//"keuzes" are the choices gotten from the database
//"keuzeAantal" is the arraylist with the amount of times the choices are in the arraylist
ArrayList<Keuze> keuzes = dakeuze.getKeuzes();
ArrayList<Keuze> keuzes2 = new ArrayList<>();
ArrayList<Integer> keuzeAantal = new ArrayList<>();
int i;
for(Keuze k : keuzes){
if (keuzes2.contains(k)){
i = keuzeAantal.get(keuzes.indexOf(k));
i++;
keuzeAantal.set(keuzes.indexOf(k),i);
} else {
keuzeAantal.add(1);
keuzes2.add(k);
}
}
request.setAttribute("toonStatistieken", "ja");
request.setAttribute("keuzes", keuzes2);
request.setAttribute("keuzeAantal", keuzeAantal);
rd = request.getRequestDispatcher("admin.jsp");
but the result on my jsp page is this (yes its in Dutch)
Attachment 3809
as you can see it puts every object in the arraylist with the amount 1
but doesnt count it.
am i overlooking something very simple and stupid, or is there an other solution?
Re: Counting objects in ArrayList
If you want help, you'll have to provide an SSCCE that demonstrates the problem. We don't know what's in those lists, and we can't copy and paste that code to run it.
But this seems like a job for a Map anyway.