Skip to Main Content

Java Programming

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

ArrayList: Calculate Mode of Integers

807603Dec 6 2007 — edited Dec 6 2007
ok I am attempting to write a function that takes the set of integers in an arraylist and calculates the mode(s) from it. I can figure out what to do to find just one mode, however I need to find all the modes. I also cannot use normal arrays to calculate the mode. I have written the other functions of my statistics library just fine so far.
import java.util.*;
import java.io.*;

public class Stat
{

    private ArrayList <Integer> list = new ArrayList <Integer> ();
    
    public Stat()
    {
        try
        {
            Scanner in = new Scanner(new File("numbers.txt"));
            ArrayList <Integer> unsortedList = new ArrayList <Integer> ();
            while(in.hasNext())
            {
                list.add(in.nextInt());
            }
        }
        catch(Exception e)
        {
            System.out.println(e.getMessage());
        }
        
    }
    
    public double median()
    {
        double temp = 0;
         for(int i=0; i < list.size(); i++)
            temp += list.get(i);
            
        temp /= list.size();
        return temp;
        
    }
    
    public double Stand()
    {
        double temp = 0;
        double median = median();
        for(int i=0; i < list.size(); i++)
            temp += Math.pow(list.get(i) - median ,2);
            
            temp /= (list.size() - 1);
            temp = Math.sqrt(temp);
            
            return temp;
            
     }
     
     public ArrayList mode()
     {
         
         ArrayList <Integer> most=new ArrayList <Integer> ();
         
        return most;
        
    }
        
        
        
    
    
}
    
    
Actual code is not needed...just some psuedocode of some kind should help. Thank you.

Edited by: jrbabb on Dec 5, 2007 9:44 PM
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jan 3 2008
Added on Dec 6 2007
2 comments
3,357 views