Results 1 to 2 of 2
Thread: Counting objects in ArrayList
- 05-17-2012, 01:15 PM #1
Member
- Join Date
- May 2012
- Posts
- 4
- Rep Power
- 0
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
Java 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)

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?
- 05-17-2012, 02:01 PM #2
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.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Add objects to ArrayList with Polymorphism
By africanhacker in forum New To JavaReplies: 7Last Post: 06-29-2011, 02:59 PM -
ArrayList with Objects the right approach?
By scheffetz in forum New To JavaReplies: 1Last Post: 03-25-2011, 09:26 AM -
Help with objects in ArrayList
By BiteMuncher in forum New To JavaReplies: 7Last Post: 02-21-2011, 11:55 AM -
Counting vowles in a collection of String objects
By sunde887 in forum New To JavaReplies: 4Last Post: 01-18-2011, 07:22 AM -
ArrayList with different objects? Help
By xtrmi in forum New To JavaReplies: 4Last Post: 02-27-2009, 08:51 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks