Results 1 to 2 of 2
  1. #1
    Gunther34567 is offline Member
    Join Date
    May 2012
    Posts
    4
    Rep Power
    0

    Default 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)

    Counting objects in ArrayList-java-probleem.png

    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?

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is online now Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default 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

  1. Add objects to ArrayList with Polymorphism
    By africanhacker in forum New To Java
    Replies: 7
    Last Post: 06-29-2011, 02:59 PM
  2. ArrayList with Objects the right approach?
    By scheffetz in forum New To Java
    Replies: 1
    Last Post: 03-25-2011, 09:26 AM
  3. Help with objects in ArrayList
    By BiteMuncher in forum New To Java
    Replies: 7
    Last Post: 02-21-2011, 11:55 AM
  4. Counting vowles in a collection of String objects
    By sunde887 in forum New To Java
    Replies: 4
    Last Post: 01-18-2011, 07:22 AM
  5. ArrayList with different objects? Help
    By xtrmi in forum New To Java
    Replies: 4
    Last Post: 02-27-2009, 08:51 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •