Results 1 to 13 of 13
  1. #1
    frozensun is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default View the most occurring value from an arraylist

    Hello,

    I have an arraylist with double values, the most occurring value (so the value that occurs most often in the arraylist) should be returned.
    My question is that someone maybe can help me to solve this. I use the program BlueJ.
    I hope someone can help me asap.
    Thanks.

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

    Default

    To get the most out of your course and this forum, you should first attempt to solve this problem yourself, and then if you run into a snag, post your code. So what have you tried?

  3. #3
    frozensun is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    I had the method to get all values into an array, but then I don't know how to get further. What I have is definately good:

    Java Code:
    public double getModas()
       {
         double[] a = new double[1480];
            for (Temp m : wd.getAllBla())
            {
               for(int index = 0; index < a.length; index++){
                 
                    a[index] = m.getCode();
                
                }
            
            }    
            
       }
    But now I have to get the value what is most occuring in the array. To make it clearlier, I want to have the Mode(statistics)(see wikipedia). I hope someone can help me further.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Put the values in a frequency Map<Integer, Temp>. So you loop through the arraylist once setting the object frequencies in the map.

  5. #5
    frozensun is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    Sorry, but I don't understand what you mean. Can you perhaps explain how do I do it? Sorry, but I am a bit new to java.

  6. #6
    frozensun is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    I have now the following, by myself:

    Java Code:
        public double getModas()
       {
         double[] a = new double[1480];
            for (Temp m : ws.getAllBla())
            {
               for(int index = 0; index<a.length; index++){
                    a[index] = m.getCode();
                
                if(a[index] == a[index]){
                    double b = (a[index]);
                //    System.out.println(b);
                
               
                }
            }
            }
        }
    I checked with System.out.println(b); that I get more values with the same content. But now I need to count how much values there are with the same content. And return that.

  7. #7
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Okay, think like this. You have an array with large number of elements, and some of them duplicated, as follows.

    {1, 2, 1, 4, 5, 1, 2, 5, 6, 8}

    You can see there numbers like 1, 2, etc are duplicated. You want to count each occurrence on each number and get the maximum occurrence. So before get the exact solution you need to count each of them and keep, and that result should be like follows.

    1 -> 3 times
    2 -> 2 times
    4 -> 1 time
    5 -> 2 times
    6 -> 1 time
    8 -> 1 time

    You have each item and the number of occurrence corresponding to. Map in Java do the same for you.

  8. #8
    frozensun is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    Ok, that's clearly how it works, thanks.
    But how do I get the value's into a Map, and read them out?

    Thanks in advance.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Here is a very simple code segment for your reference. Carefully go through it.

    Java Code:
            int[] myArray = {1, 2, 1, 4, 5, 1, 2, 5, 6, 8};
            TreeMap <Integer, Integer> tMap = new TreeMap<Integer, Integer>();
    
            for(int index = 0; index < myArray.length; index++) {
    
                if(tMap.get(myArray[index]) == null) {
                    tMap.put(myArray[index], 1);
                }
                else {
                    int val = tMap.get(myArray[index]);
                    tMap.put(myArray[index], ++val);
                }
            }
    
            System.out.println(tMap.keySet());
            System.err.println(tMap.values());

  10. #10
    frozensun is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    Thank you!
    And how do I now return the most occuring number?(here it is 1)
    And my value's in the array are doubles, I don't know of it matters.

  11. #11
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Did you read the API?

    Using simple logic you can do that, loop through each item and check for the maximum value at least.

  12. #12
    frozensun is offline Member
    Join Date
    Oct 2009
    Posts
    7
    Rep Power
    0

    Default

    But you can't put doubles in the Map? It gives an error with tMap.put(a[index], 1); because a[index] is a double..

  13. #13
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by frozensun View Post
    But you can't put doubles in the Map? It gives an error with tMap.put(a[index], 1); because a[index] is a double..
    You should learn the basis first. I just give you an example, if you don't know how to change accordingly at least, better to keep this for min away and study about the basis.

Similar Threads

  1. A view to help
    By hervey in forum Reviews / Advertising
    Replies: 1
    Last Post: 10-29-2008, 07:37 AM
  2. Updating a view using actions in a seperate view
    By xcallmejudasx in forum Eclipse
    Replies: 0
    Last Post: 10-24-2008, 09:24 PM
  3. Replies: 2
    Last Post: 04-21-2008, 11:43 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
  •