Top Banner

of 17

Create the Set

Apr 06, 2018

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
  • 8/3/2019 Create the Set

    1/17

    // Create the set

    Set set = new HashSet();

    // Add elements to the set

    set.add("a");

    set.add("b");

    set.add("c");

    // Remove elements from the set

    set.remove("c");

    // Get number of elements in set

    int size = set.size(); // 2

    // Adding an element that already exists in the set has no effect

    set.add("a");

    size = set.size(); // 2

    // Determining if an element is in the set

    boolean b = set.contains("a"); // true

    b = set.contains("c"); // false

    // Iterating over the elements in the set

    Iterator it = set.iterator();

    while (it.hasNext()) {

    // Get element

    Object element = it.next();

    }

    Method Summaryboolean add(Object o)

    Adds the specified element to this set if it is not already present (optional operation)

    boolean addAll(Collection c)

    Adds all of the elements in the specified collection to this set if they're not alreadypresent (optional operation).

    void clear()

    Removes all of the elements from this set (optional operation).

    boolean contains(Object o)

    Returns true if this set contains the specified element.

    boolean containsAll(Collection c)

    Returns true if this set contains all of the elements of the specified collection.

    boolean equals(Object o)

    Compares the specified object with this set for equality.

    http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#add(java.lang.Object)http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#addAll(java.util.Collection)http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collection.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#clear()http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#contains(java.lang.Object)http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#containsAll(java.util.Collection)http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collection.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#equals(java.lang.Object)http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#add(java.lang.Object)http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#addAll(java.util.Collection)http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collection.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#clear()http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#contains(java.lang.Object)http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#containsAll(java.util.Collection)http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collection.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#equals(java.lang.Object)http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.html
  • 8/3/2019 Create the Set

    2/17

    int hashCode()

    Returns the hash code value for this set.

    boolean isEmpty()

    Returns true if this set contains no elements.

    Iterator iterator()Returns an iterator over the elements in this set.

    boolean remove(Object o)

    Removes the specified element from this set if it is present (optional operation).

    boolean removeAll(Collection c)

    Removes from this set all of its elements that are contained in the specifiedcollection (optional operation).

    boolean retainAll(Collection c)

    Retains only the elements in this set that are contained in the specified collection

    (optional operation).int size()

    Returns the number of elements in this set (its cardinality).

    Object[] toArray()

    Returns an array containing all of the elements in this set.

    Object[] toArray(Object[] a)

    Returns an array containing all of the elements in this set; the runtime type of thereturned array is that of the specified array.

    What is the type of the objects in your

    arrays? You want compare the object'svalue or just the object(the reference)

    itself?EmptyStackAug 29 '11 at 10:47

    both arrays contains strings, I want to put common strings to a new

    arraysajazAug 29 '11 at 10:55

    Alright. Try this solution.stackoverflow.com/questions/7207805/

    EmptyStackAug 29 '11 at 10:58

    @Vijay you copied your own answer and you link to it. Wouldn't be

    one enough?vikingosegundoAug 29 '11 at 11:17

    @viking just now i answered for both of them.by read from his comments i

    realize that he has nsstring in both.so that i have route him to that link.butbefore that i had thought any object he wants.nothing

    elseAppleVijayAug 29 '11 at 11:21

    compare

    SArray *array1 = [[NSArray alloc] initWithObjects:@"a",@"b",@"c",nil];

    NSArray *array2 = [[NSArray alloc] initWithObjects:@"a",@"d",@"c",nil];

    http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#hashCode()http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#isEmpty()http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Iterator.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#iterator()http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#remove(java.lang.Object)http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#removeAll(java.util.Collection)http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collection.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#retainAll(java.util.Collection)http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collection.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#size()http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#toArray()http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#toArray(java.lang.Object[])http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.htmlhttp://stackoverflow.com/users/491980/emptystackhttp://stackoverflow.com/users/491980/emptystackhttp://stackoverflow.com/questions/7229046/compare-two-arrays-and-put-equal-objects-to-new-array#comment8691508_7229046http://stackoverflow.com/questions/7229046/compare-two-arrays-and-put-equal-objects-to-new-array#comment8691508_7229046http://stackoverflow.com/users/627667/sajazhttp://stackoverflow.com/questions/7229046/compare-two-arrays-and-put-equal-objects-to-new-array#comment8691624_7229046http://stackoverflow.com/questions/7207805/getting-unique-numbers-from-two-arrays/7207883#7207883http://stackoverflow.com/questions/7207805/getting-unique-numbers-from-two-arrays/7207883#7207883http://stackoverflow.com/users/491980/emptystackhttp://stackoverflow.com/questions/7229046/compare-two-arrays-and-put-equal-objects-to-new-array#comment8691670_7229046http://stackoverflow.com/users/106435/vikingosegundohttp://stackoverflow.com/questions/7229046/compare-two-arrays-and-put-equal-objects-to-new-array#comment8691924_7229046http://stackoverflow.com/users/675170/applevijayhttp://stackoverflow.com/users/675170/applevijayhttp://stackoverflow.com/questions/7229046/compare-two-arrays-and-put-equal-objects-to-new-array#comment8691978_7229046http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#hashCode()http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#isEmpty()http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Iterator.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#iterator()http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#remove(java.lang.Object)http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#removeAll(java.util.Collection)http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collection.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#retainAll(java.util.Collection)http://docs.oracle.com/javase/1.4.2/docs/api/java/util/Collection.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#size()http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#toArray()http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.htmlhttp://docs.oracle.com/javase/1.4.2/docs/api/java/util/Set.html#toArray(java.lang.Object[])http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Object.htmlhttp://stackoverflow.com/users/491980/emptystackhttp://stackoverflow.com/questions/7229046/compare-two-arrays-and-put-equal-objects-to-new-array#comment8691508_7229046http://stackoverflow.com/users/627667/sajazhttp://stackoverflow.com/questions/7229046/compare-two-arrays-and-put-equal-objects-to-new-array#comment8691624_7229046http://stackoverflow.com/questions/7207805/getting-unique-numbers-from-two-arrays/7207883#7207883http://stackoverflow.com/questions/7207805/getting-unique-numbers-from-two-arrays/7207883#7207883http://stackoverflow.com/users/491980/emptystackhttp://stackoverflow.com/questions/7229046/compare-two-arrays-and-put-equal-objects-to-new-array#comment8691670_7229046http://stackoverflow.com/users/106435/vikingosegundohttp://stackoverflow.com/questions/7229046/compare-two-arrays-and-put-equal-objects-to-new-array#comment8691924_7229046http://stackoverflow.com/users/675170/applevijayhttp://stackoverflow.com/questions/7229046/compare-two-arrays-and-put-equal-objects-to-new-array#comment8691978_7229046
  • 8/3/2019 Create the Set

    3/17

    NSMutableArray *ary_result = [[NSMutableArray alloc] init];

    for(int i = 0;i

  • 8/3/2019 Create the Set

    4/17

  • 8/3/2019 Create the Set

    5/17

    frequency[i] = 0 ;}

    o 7Step through each value in your data set, adding one to the frequency array element

    corresponding to that value. The index corresponding to the current value is generated byshifting the value by the minimum value.for (i = 0 ; i < numvalues ; i++) {int index = values[i] - minval ;frequency[index]++ ;}

    o 8Step through each element in the frequency array. Print the current value (calculated byshifting the iterator "i" by the minimum value). Print the number of stars (*) corresponding tothe frequency the current value by looping from one to the value stored in the frequencyarray, printing a single star each time.

    for (i = 1 ; i

  • 8/3/2019 Create the Set

    6/17

    h.put("NY", "New York");h.put("RI", "Rhode Island");h.put("BC", "British Columbia");

    // look up a key in the HashtableString key = "NY";String stateName = (String)h.get(key );System.out.println(stateName );

    // prints "New York"

    // enumerate all the contents of the hashtableEnumeration keys = h.keys();

    while (keys.hasMoreElements()) { key = (String)keys.nextElement(); stateName = (String)h.get(key ); System.out.println(key + " "+ stateName ); // prints lines of the form NY New York // in effectively random order. }// end while

    Histogram.java

    Below is the syntax highlighted version ofHistogram.java from 3.2 Creating

    Data Types.

    /

    ***********************************************************************

    **

    * Compilation: javac Histogram.java

    *

    * This data type supports simple client code to create dynamic

    * histograms of the frequency of occurrence of values in [0, N).

    * The frequencies are kept in an instance-variable array, and

    * an instance variable max tracks the maximum frequency (for

    scaling).

    http://introcs.cs.princeton.edu/java/32class/Histogram.javahttp://introcs.cs.princeton.edu/32classhttp://introcs.cs.princeton.edu/32classhttp://introcs.cs.princeton.edu/java/32class/Histogram.javahttp://introcs.cs.princeton.edu/32classhttp://introcs.cs.princeton.edu/32class
  • 8/3/2019 Create the Set

    7/17

    *

    * % java Histogram 50 1000000

    *

    **********************************************************************

    ***/

    publicclass Histogram {

    privatefinaldouble[] freq; // freq[i] = # occurences of value i

    privatedouble max; // max frequency of any value

    // Create a new histogram.

    publicHistogram(int N) {

    freq =newdouble[N];

    }

    // Add one occurrence of the value i.

    publicvoidaddDataPoint(int i) {

    freq[i]++;

    if(freq[i]> max) max = freq[i];

    }

    // draw (and scale) the histogram.

    publicvoiddraw() {

    StdDraw.setYscale(0, max);

    StdStats.plotBars(freq);

    }

    // See Program 2.2.6.

    publicstaticvoidmain(String[] args) {

    int N = Integer.parseInt(args[0]); // number of coins

    int T = Integer.parseInt(args[1]); // number of trials

    // create the histogram

    Histogram histogram =newHistogram(N+1);

    for(int t =0; t < T; t++) {

    histogram.addDataPoint(Bernoulli.binomial(N));

    }

    // display using standard draw

    StdDraw.setCanvasSize(500,100);

    histogram.draw();

    }

    }

    Copyright 20002010, Robert Sedgewick and Kevin Wayne.Last updated: Wed Feb 9 09:07:43 EST 2011.

    Histogram printing program

    import javax.swing.*;

    public class Histogram

    {

  • 8/3/2019 Create the Set

    8/17

    public static void main( String args[] )

    {

    int n[] = { 19, 3, 15, 7, 11, 9, 13, 5, 17, 1 };String output = "";

    output += "Element\tValue\tHistogram";

    for ( int i = 0; i < n.length; i++ )

    {output += "\n" + i + "\t" + n[ i ] + "\t";

    for ( int j = 1; j

  • 8/3/2019 Create the Set

    9/17

    JavaRanch Java Forums Java Beginning Java

    Author

    Displaying a histogram using input fro

    array

    kanaka tamRanch Hand

    Joined: Jan 19,

    2004Posts: 42

    posted Friday, April 15, 2005 23:02

    Hello folks,

    I am currently working on a problem that inputs 20 numbers into an array and the n

    range of 0 to 100. I had to calculate smallest, largest, mean and average of all the n

    which i was able to do without any problem. But the second half of the problem is to

    show how many numbers are in the ranges of 0 to 9, 10 to 19 etc., This is a class as

    read the instructions ofjava ranch. So i just need a little head start and not expectin

    me. This is how far i have gotten with the code.

    view plaincopy to clipboardprint?

    1. import java.awt.*;

    2. import java.applet.*;3.

    4.

    5. publicclass ArrayTest extends Applet

    6.7. {

    8. privateint [] num = {5, 7, 8, 19, 25, 30, 37, 41, 43, 50, 52, 55

    9. 61, 63, 70, 76, 84, 91, 95, 98};10.

    11.

    12. privateint max, min, sum;

    13. privatefloat average;14.

    15. publicvoid largestSmallest()16. {

    17.

    18. max = -1;

    19. min = 101;20.

    21. for (int i = 0; i < num.length; i++)22. {

    23. if (max < num[i])24. {

    25. max = num[i];

    26. }

    http://www.javaranch.com/http://www.coderanch.com/forumshttp://www.coderanch.com/forumshttp://www.coderanch.com/forums/c/1/javahttp://www.coderanch.com/forums/f-33/javahttp://www.javaranch.com/http://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.javaranch.com/http://www.coderanch.com/forumshttp://www.coderanch.com/forums/c/1/javahttp://www.coderanch.com/forums/f-33/javahttp://www.javaranch.com/http://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-array
  • 8/3/2019 Create the Set

    10/17

    27. if (min > num[i])28. {

    29. min = num[i];

    30. }

    31. }

    32.

    33. }34.

    35. publicvoid sumAverage()36. {

    37. sum = 0;

    38. average = 0.0f;39.

    40. for (int i = 0; i < num.length; i++)41. {

    42. sum = sum + num[i];

    43. average = (float)(sum) / num.length;44. }

    45.

    46. }47.

    48.

    49.

    50. publicvoid paint (Graphics g)51. {

    52. largestSmallest();

    53. sumAverage();

    54.

    55. g.drawString("The largest number in this array is: " + max, 30

    56. g.drawString("The smallest number in this array is: " + min,

    57. g.drawString("The sum of the numbers in this array is: " + s

    58. g.drawString("The mean of the numbers in this array is: " + av59.60.

    61. }

    62.

    63. }

    64.

    I am lost in figuring out how to do the histogram for this problem. Any help appreciat

    know how to approach this portion of the problem,

    Thanx,

    kt

    Ryan McGuireRanch Hand

    Joined: Feb 18,2005

    Posts: 864

    posted Saturday, April 16, 2005 01:14

    Make the histogram in two steps:

  • 8/3/2019 Create the Set

    11/17

    1. Collect the numbers into some data structure (most likely an array).

    2. Output the results.

    Are you supposed to use something like System.out.println() to output a row of aster

    or do you have to do something more graphical?

    Ryan

    kanaka tamRanch Hand

    Joined: Jan 19,2004

    Posts: 42

    posted Saturday, April 16, 2005 02:09

    Originally posted by Ryan McGuire:

    Make the histogram in two steps:

    1. Collect the numbers into some data structure (most likely an array).

    2. Output the results.

    Are you supposed to use something like System.out.println() to output a row of asterisks

    you have to do something more graphical?

    Ryan

    Thank you Ryan. I might have to use an array to collect the range say 0 to 9, 10 to 1

    recently introduced to arrays and i don't know how to initialize arrays for a range of n

    I am thinking like this, but not sure

    private int [] output = {0 - 9, 10 - 19, 20 - 29, 30 - 39, 40 - 49, 50 - 59, 60 - 69, 70

    99};

    Then if this format is right i have to figure out where my 20 numbers that i have inpu

    array exists, that is in what range. I have to use filled rectangles to get a histogram t

    many numbers are in the range 0 to 9, 10 to 19 etc.,

    How would i check what number is in what range. I know the procedure but not sure

    Help appreciated..

    kanaka

    Ryan McGuire posted Saturday, April 16, 2005 02:27

  • 8/3/2019 Create the Set

    12/17

    Ranch Hand

    Joined: Feb 18,

    2005Posts: 864

    (edit: remove quote)

    That array declaration may cause you some trouble. 0-9 = -9. 10-19 = -9, etc. So yo

    position array with each position initialized to negative nine.

    I think it would be better if you initialized your array to all zeros. And then go throug

    numbers and increment one of the ints in output.

    Given any input number in the range [0, 99], can you give me an expression or a me

    which int in output to increment? For example, if you see a 22, you want to incremen

    sure the expression or method works for input numbers that are near the ends of you

    and 30.)

    Ryan

    [ April 15, 2005: Message edited by: Ryan McGuire ]

    kanaka tamRanch Hand

    Joined: Jan 19,

    2004Posts: 42

    posted Saturday, April 16, 2005 06:32

    Originally posted by Ryan McGuire:

    (edit: remove quote)

    That array declaration may cause you some trouble. 0-9 = -9. 10-19 = -9, etc. So you'll e

    array with each position initialized to negative nine.

    I think it would be better if you initialized your array to all zeros. And then go through you

    and increment one of the ints in output.

    Given any input number in the range [0, 99], can you give me an expression or a method

    int in output to increment? For example, if you see a 22, you want to increment output[2]

    expression or method works for input numbers that are near the ends of your ranges, suc

    Ryan

  • 8/3/2019 Create the Set

    13/17

    [ April 15, 2005: Message edited by: Ryan McGuire ]

    Thanx Ryan,

    I added the following, no compilation errors but cannot see any histogram output. Co

    am i doing wrong. Here is the code.view plaincopy to clipboardprint?

    1.

    2. import java.awt.*;

    3. import java.applet.*;4.

    5.

    6. publicclass ArrayTest extends Applet7.8. {

    9. privateint [] num = {5, 7, 8, 19, 25, 30, 37, 41, 43, 50, 52, 55

    10. 61, 63, 70, 76, 84, 91, 95, 98};

    11. privateint [] hist = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};12.

    13.

    14. privateint max, min, sum;

    15. privatefloat average;16.

    17. publicvoid largestSmallest()18. {

    19.

    20. max = -1;

    21. min = 101;22.

    23. for (int i = 0; i < num.length; i++)24. {

    25. if (max < num[i])26. {

    27. max = num[i];

    28. }

    29. if (min > num[i])30. {

    31. min = num[i];

    32. }33. }

    34.

    35. }

    36.

    37. publicvoid sumAverage()38. {

    39. sum = 0;

    40. average = 0.0f;

    http://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-arrayhttp://www.coderanch.com/t/399326/java/java/Displaying-histogram-input-array
  • 8/3/2019 Create the Set

    14/17

    41.

    42. for (int i = 0; i < num.length; i++)43. {

    44. sum = sum + num[i];

    45. average = (float)(sum) / num.length;46. }

    47.48. }

    49.

    50. publicvoid drawHist()51. {

    52. for( int i = 0; i

  • 8/3/2019 Create the Set

    15/17

    93.

    94. g.drawString("The largest number in this array is: " + max, 10

    95. g.drawString("The smallest number in this array is: " + min,

    96. g.drawString("The sum of the numbers in this array is: " + s

    97. g.drawString("The mean of the numbers in this array is: " + av

    98.99. int x = 20;

    100. int y = 20;

    101. int height = 40;102.

    103.

    104. for(int i = 0; i < hist.length; i++)105. {

    106. drawHist();

    107. int width = hist[i];

    108. g.fillRect(x, y, width * 10, height);109.

    110. }

    111. x = x + 10;112. }

    113.

    114. }

    115.

    I would appreciate any help.

    thanx,

    kt

    subject: Displaying a histogram using input from an array

    All times above are in your local time zone & format.The current ranch time (not your local time) is Feb 16, 2012 03:29:35.

    Contact Us | Powered by JForum | Copyright 1998-2012 Paul Wheaton

    http://www.coderanch.com/how-to/code/ContactUshttp://www.jforum.net/http://www.javaranch.com/paul-wheaton.jsphttp://www.coderanch.com/forums/banner/redirect/300http://www.coderanch.com/how-to/code/ContactUshttp://www.jforum.net/http://www.javaranch.com/paul-wheaton.jsp
  • 8/3/2019 Create the Set

    16/17

    more from paul wheaton's glorious empire of web junk: castiron skilletdiatomaceous earthrocket mass heatersepp

    holzerraised garden beds raising chickenslawn careCFLfleacontrolmissoulaheatpermaculture

    Yes, there does seem to be 3rd libraries (none in Java Math). Two that have come up are:

    http://opsresearch.com/app/

    http://www.iro.umontreal.ca/~simardr/ssj/indexe.htmlbut, it is actually not that difficult to write your own methods to calculate mean, median, mode

    and range.

    MEAN

    publicstaticdouble mean(double[] m) {

    double sum = 0;

    for (int i = 0; i < double.length; i++) {

    sum += m[i];

    }

    return sum / double.length;

    }

    MEDIAN

    // the array double[] m MUST BE SORTED

    publicstaticdouble median(double[] m) {

    int middle = m.length/2;

    if(m.length%2 == 1) {

    return m[middle];

    } else {

    return (m[middle-1] + m[middle]) / 2.0;

    }}

    MODE

    publicstaticint mode(int a[]) {

    int maxValue, maxCount;

    http://www.richsoil.com/cast-iron.jsphttp://www.richsoil.com/cast-iron.jsphttp://www.richsoil.com/diatomaceous-earth.jsphttp://www.richsoil.com/rocket-stove-mass-heater.jsphttp://www.richsoil.com/sepp-holzer/sepp-holzer-permaculture.jsphttp://www.richsoil.com/sepp-holzer/sepp-holzer-permaculture.jsphttp://www.richsoil.com/hugelkultur/http://www.richsoil.com/raising-chickens.jsphttp://www.richsoil.com/lawn-care.jsphttp://www.richsoil.com/CFL-fluorescent-light-bulbs.jsphttp://www.richsoil.com/flea-control.jsphttp://www.richsoil.com/flea-control.jsphttp://www.permies.com/missoulahttp://www.richsoil.com/electric-heat.jsphttp://www.permies.com/permaculture-forumshttp://opsresearch.com/app/http://www.iro.umontreal.ca/~simardr/ssj/indexe.htmlhttp://www.richsoil.com/cast-iron.jsphttp://www.richsoil.com/cast-iron.jsphttp://www.richsoil.com/diatomaceous-earth.jsphttp://www.richsoil.com/rocket-stove-mass-heater.jsphttp://www.richsoil.com/sepp-holzer/sepp-holzer-permaculture.jsphttp://www.richsoil.com/sepp-holzer/sepp-holzer-permaculture.jsphttp://www.richsoil.com/hugelkultur/http://www.richsoil.com/raising-chickens.jsphttp://www.richsoil.com/lawn-care.jsphttp://www.richsoil.com/CFL-fluorescent-light-bulbs.jsphttp://www.richsoil.com/flea-control.jsphttp://www.richsoil.com/flea-control.jsphttp://www.permies.com/missoulahttp://www.richsoil.com/electric-heat.jsphttp://www.permies.com/permaculture-forumshttp://opsresearch.com/app/http://www.iro.umontreal.ca/~simardr/ssj/indexe.html
  • 8/3/2019 Create the Set

    17/17

    for (int i = 0; i < a.length; ++i) {

    int count = 0;

    for (int j = 0; j < a.length; ++j) {

    if(a[j] == a[i]) ++count;

    }

    if(count > maxCount) {maxCount = count;

    maxValue = a[i];

    }

    }

    return maxValue;

    }