中国开发网: 论坛: 程序员情感CBD: 贴子 230935
nostalgic
算法
import java.util.*;

public class MapTest {
private static String names[] = { "a", "b", "e",
"d", "f", "a", "c", "d", "f", "f" };

// build a HashMap and output contents
public MapTest()
{
HashMap map = new HashMap();
Integer i;

for ( int count = 0; count < names.length; count++ ) {
i = ( Integer ) map.get(
new Character( names[ count ].charAt( 0 ) ) );

// if key is not in map then give it value one
// otherwise increment its value by 1
if ( i == null )
map.put(
new Character( names[ count ].charAt( 0 ) ),
new Integer( 1 ) );
else
map.put(
new Character( names[ count ].charAt( 0 ) ),
new Integer( i.intValue() + 1 ) );
}


printMap( map );
}

// output map contents
public void printMap( Map mapRef )
{
System.out.println( mapRef.toString() );

}

// execute application
public static void main( String args[] )
{
new MapTest();
}

} // end class MapTest

相关信息:


欢迎光临本社区,您还没有登录,不能发贴子。请在 这里登录