ERLEDIGT
NEIN
NEIN
ANTWORTEN
2
2
ZUGRIFFE
1854
1854
EMPFEHLEN
-
Hallo zusammen,
ich habe folgendes Array: {1,1,1,1,2,2,2,3,4,4,4,4,4,4,4,4,5,5}
Und möchte nun errechnen, welche Zahl am häufigsten vorkommt, also als Ergebnis die 4 haben.
thx4help
-
28.06.09 22:19 #2
- Registriert seit
- Jun 2002
- Ort
- Saarbrücken (Saarland)
- Beiträge
- 9.885
- Blog-Einträge
- 29
Hallo,
schau mal hier:
(
http://www.tutorials.de/forum/java/3...ualiseren.html )
Code java:1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
package de.tutorials; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.NavigableSet; import java.util.TreeMap; import java.util.TreeSet; import java.util.Map.Entry; public class RunningStatisticsExample { public static void main(String[] args) { Double[] values = { 1., 1., 1., 1., 2., 2., 2., 3., 4., 4., 4., 4., 4., 4., 4., 4., 5., 5. }; Statistics statistics = new Statistics(); for (int i = 0; i < values.length; i++) { statistics.update(values[i]); } System.out.println(statistics); System.out.println("modus: " + Statistics.modusOf(values)); } static class Statistics { double mean; double standardDeviation; double variance; double sumOfSquanres; double sum; int count; private void update(double value) { count++; this.sum += value; this.sumOfSquanres += value * value; this.mean += (value - mean) / count; this.standardDeviation = Math.sqrt((count * sumOfSquanres - sum * sum) / (count * (count - 1))); this.variance = this.standardDeviation * this.standardDeviation; } public static <T extends Comparable<T>> T modusOf(T... values) { Histogram<T> hist = new Histogram<T>(Arrays.asList(values)); //hist.printSorted(); return hist.modus(); } public double getMean() { return mean; } public void setMean(double mean) { this.mean = mean; } public double getStandardDeviation() { return standardDeviation; } public void setStandardDeviation(double standardDeviation) { this.standardDeviation = standardDeviation; } @Override public String toString() { StringBuilder stringBuilder = new StringBuilder(); stringBuilder.append("sum: "); stringBuilder.append(sum); stringBuilder.append("\n"); stringBuilder.append("sum of x²: "); stringBuilder.append(sumOfSquanres); stringBuilder.append("\n"); stringBuilder.append("mean: "); stringBuilder.append(mean); stringBuilder.append("\n"); stringBuilder.append("stddev: "); stringBuilder.append(standardDeviation); stringBuilder.append("\n"); stringBuilder.append("variance: "); stringBuilder.append(variance); stringBuilder.append("\n"); return stringBuilder.toString(); } static class Histogram<TValue extends Comparable<TValue>> { Map<TValue, Integer> histogram; Comparator<Map.Entry<TValue, Integer>> comparator = new Comparator<Map.Entry<TValue, Integer>>() { public int compare(Entry<TValue, Integer> left, Entry<TValue, Integer> right) { int result = left.getValue().compareTo(right.getValue()); return result == 0 ? left.getKey().compareTo(right.getKey()) : result; } }; public Histogram(List<TValue> comparables) { this.histogram = new TreeMap<TValue, Integer>(); for (TValue comparable : comparables) { this.histogram.put(comparable, Collections.frequency(comparables, comparable)); } } NavigableSet<Map.Entry<TValue, Integer>> sorted() { NavigableSet<Map.Entry<TValue, Integer>> set = new TreeSet<Entry<TValue, Integer>>(comparator); set.addAll(this.histogram.entrySet()); return set; } public TValue modus(){ return sorted().last().getKey(); } public void print() { System.out.println(histogram); } public void printSorted() { System.out.println(sorted()); } } } }
Ausgabe:
Code :1 2 3 4 5 6 7
sum: 55.0 sum of x²: 203.0 mean: 3.055555555555556 stddev: 1.4337208778404378 variance: 2.0555555555555554 modus: 4.0
Gruß TomJava rocks!
How to become a good Java Programmer?
Does IT in Java and .Net
The only valid measurement of code quality: WTFs / minute
Blog
Xing
Twitter
-
Ginge auch mit dem Math Package (Apache-Commons).
Code :1 2 3 4 5 6 7 8 9 10 11 12 13
package math; import org.apache.commons.math.stat.descriptive.rank.Median; public class ModalValue { public static void main(String... args) { double[] values = { 1., 1., 1., 1., 2., 2., 2., 3., 4., 4., 4., 4., 4., 4., 4., 4., 5., 5. }; Median tmpMed = new Median(); System.out.println("Median: " + tmpMed.evaluate(values)); } }
Ähnliche Themen
-
Point-Modus
Von sight011 im Forum Cinema 4DAntworten: 10Letzter Beitrag: 04.07.08, 18:19 -
Was ist der STOCHASTISCHE Modus (AR)
Von saschahaeusler im Forum Cinema 4DAntworten: 6Letzter Beitrag: 28.08.05, 18:07 -
abgesicherter Modus
Von beruwe im Forum Microsoft WindowsAntworten: 3Letzter Beitrag: 17.12.04, 14:33 -
Dos Modus
Von morph-x im Forum Microsoft WindowsAntworten: 5Letzter Beitrag: 24.07.04, 11:14 -
PHP im Debug-Modus - wie?
Von ufoman2 im Forum PHPAntworten: 2Letzter Beitrag: 02.07.01, 23:06





Zitieren

Login





