Results 1 to 5 of 5
  1. #1
    Stamoulohta is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default Count boolean Occurrences

    Hello to all,

    is there a way in java to count occurrences of boolean values in an array?

    eg:
    Java Code:
    boolean[] list = new boolean[5];
    for(int i=0; i<5; i++){
        if(i % 2 == 0) list[i] = true;
    }
                      // this is pythonian pseudocode
    list.count(true); // but it is what I'm looking for...
    Thank you,
    George.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    Java Code:
    boolean[] list = new boolean[5];
    for(int i=0; i<list.length; i++){
      if(i%2==0) list[i] = true;
    }
    
    int countTrue = 0;
    
    for (boolean b:list) {
     //if true: increment counter;
     if(b) countTrue++;
    }
    to count both true and false change the for-each loop to:

    Java Code:
    int countFalse = 0;
    
    for (boolean b:list) {
     b?countTrue++:countFalse++;
    }

    But it seems like you only want to add items to this list if a condition is true, which you can do if you use an ArrayList e.g.

    Java Code:
    ArrayList myArray = new ArrayList();
    
    for (i=0;i<5;i++) {
     if (i%2==0) myArray.add(true);
    }
    
    int count = myArray.size();
    Last edited by ozzyman; 03-13-2011 at 04:02 PM.

  4. #4
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    5

    Default

    Or use collections instead of arrays, there is a method Collections.frequency which do that what do you want! (or if you have an array of boolean objects -> Boolean[] list = new Boolean[5]; you could use Collections.frequency(Arrays.asList(list), true) )


    or (not a serious suggestion):
    Arrays.toString(list).replaceAll("[, \\[\\]]|false", "").length()/"true".length()


    :D
    Last edited by eRaaaa; 03-13-2011 at 04:10 PM.

  5. #5
    Stamoulohta is offline Member
    Join Date
    Mar 2011
    Posts
    26
    Rep Power
    0

    Default

    thank you guys,

    I appreciate your help very much.

    I have another question though:
    Why are all these handy builtins missing from java? Don't get me wrong, I mean no disrespect for the language, if I did, I wouldn't be trying to learn it..
    but every time I try learn how to do something, the answer is always "the long way"!!

    Is there a reason for that or we could hope to see some nice builtins in next versions of java?

Similar Threads

  1. Lucene serch (no of occurrences)
    By vaibhav.arya in forum Lucene
    Replies: 1
    Last Post: 01-19-2011, 08:02 PM
  2. use Math.random and then count occurrences
    By iliak67 in forum New To Java
    Replies: 17
    Last Post: 11-16-2009, 04:57 PM
  3. Number of occurrences in a int[ ]
    By random0munky in forum New To Java
    Replies: 10
    Last Post: 10-10-2009, 10:22 PM
  4. Replies: 20
    Last Post: 05-14-2008, 09:42 AM

Posting Permissions

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