Calculate median of arraylist

2015-01-13

azim58 - Calculate median of arraylist


3-4-13 added the ability to calculate the median of an arraylist to my
UsefulTools class without use of Expression class class

code here


===========================================================================

public static double Median(ArrayList values)

//first make sure we are dealing with a list of doubles
if(!values.get(0).getClass().equals("class java.lang.Double"))
{
values = convertArrayListOfStringsToDoubles(values);





Collections.sort(values);
System.out.println(values);
if (values.size() % 2 == 1)
return
Double.valueOf(String.valueOf(values.get((values.size()+1)/2-1)).toString()
).doubleValue();
else

double lower =
Double.valueOf(String.valueOf(values.get(values.size()/2-1)).toString()).do
ubleValue();
double upper =
Double.valueOf(String.valueOf(values.get(values.size()/2)).toString()).doub
leValue();


return (lower + upper) / 2.0;

}


public static ArrayList convertArrayListOfStringsToDoubles(ArrayList
values)

for(int i=0; i<values.size(); i++)
{
values.set(i, Double.valueOf(values.get(i).toString()).doubleValue());

return values;
}


===========================================================================








public static double Median(ArrayList values)

Collections.sort(values);

if (values.size() % 2 == 1)
return
Double.valueOf(String.valueOf(values.get((values.size()+1)/2-1)).toString()
).doubleValue();
else
{
double lower =
Double.valueOf(String.valueOf(values.get(values.size()/2-1)).toString()).do
ubleValue();
double upper =
Double.valueOf(String.valueOf(values.get(values.size()/2)).toString()).doub
leValue();

return (lower + upper) / 2.0;

}