度数分布
戻る
::::::::::::::
MyComparator.java
::::::::::::::
import java.util.*;
/**
$Id: frequencyDistribution.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public class MyComparator implements Comparator {
public int compare( Object a, Object b ) {
Integer[] integerA = ( Integer[] ) a;
Integer[] integerB = ( Integer[] ) b;
return ( integerA[ 1 ].compareTo( integerB[ 1 ] ) );
}
}
::::::::::::::
RandomTest.java
::::::::::::::
import java.util.*;
/**
$Id: frequencyDistribution.html,v 1.1 2009/06/22 16:12:10 kishi Exp kishi $
@author KISHI Yasuhiro
*/
public class RandomTest {
static public void main( String[] args ) {
final int NUMBER_OF_OCCURRENCES = 128;
final int ELEMENT_COUNT = 16;
Random random = new Random();
HashMap hm = new HashMap();
for ( int i = 0;i < NUMBER_OF_OCCURRENCES;i++ ) {
int result = random.nextInt( ELEMENT_COUNT );
Integer key = new Integer( result );
if ( hm.containsKey( key ) ) {
Integer count = ( Integer ) hm.get( key );
hm.put( key, new Integer( count.intValue() + 1 ) );
} else {
hm.put( key, new Integer( 1 ) );
}
}
/** 発生頻度順で昇順ソートする **/
/** 同時に度数分布も求める **/
TreeMap frequencyDistribution = new TreeMap();
int min = -1;
int max = -1;
// Integer型の2次元配列を作成
Iterator iterator = hm.keySet().iterator();
Integer[][] array = new Integer[ ELEMENT_COUNT ][];
for ( int i = 0;i < ELEMENT_COUNT; i++ ) {
array[ i ] = new Integer[ 2 ];
if ( iterator.hasNext() ) {
Integer key = ( Integer ) iterator.next();
Integer count = ( Integer ) hm.get( key );
array[ i ][ 0 ] = key;
array[ i ][ 1 ] = count;
// 発生頻度の最小値を求める
if ( min == -1 ) {
min = count.intValue();
} else {
if ( min > count.intValue() ) {
min = count.intValue();
}
}
// 発生頻度の最大値を求める
if ( max == -1 ) {
max = count.intValue();
} else {
if ( max < count.intValue() ) {
max = count.intValue();
}
}
//=============================
// 度数分布を求める
//=============================
if ( frequencyDistribution.containsKey( count ) ) {
Integer frequency = ( Integer ) frequencyDistribution.get( count );
frequencyDistribution.put( count, new Integer( frequency.intValue() + 1 ) );
} else {
frequencyDistribution.put( count, new Integer( 1 ) );
}
}
}
//========================================================
// ソート実施
//========================================================
MyComparator comparator = new MyComparator();
Arrays.sort( array, comparator );
/** 結果の一覧表示 */
for ( int i = 0;i < array.length;i++ ) {
for ( int j = 0; j < array[ i ].length;j++ ) {
System.out.print( "\t" + array[ i ][ j ] );
}
System.out.println();
}
System.out.println( "最小値: " + min );
System.out.println( "最大値: " + max );
//========================================================
// 度数分布を出力
//========================================================
System.out.println( "---------------------------------------------------------------" );
System.out.println( "【度数分布】" );
System.out.println( "---------------------------------------------------------------" );
for ( int i = min;i <= max;i++ ) {
Integer frequency = ( Integer ) frequencyDistribution.get( new Integer( i ) );
int occurrence;
if ( frequency == null ) {
occurrence = 0;
} else {
occurrence = frequency.intValue();
}
System.out.print( i + ": " + occurrence + "\t" );
for ( int j = 0;j < occurrence;j++ ) {
System.out.print( "*" );
}
System.out.println();
}
}
}
戻る