给定["a","b","a","b","c","a","b","c","b"]字符串数组,然后使用Map的key来保存数组的字符串元素,value保存该字符串元素出现的次数。

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_31884013/article/details/49682393
import java.util.HashMap;

public class p3 {

	public static void main(String[] args) {
		HashMap hM=new HashMap();
		String[] sz={
				"a","b","a","b","c","a","b","c","b"
		};
                
		for(int i=0;i<sz.length;i++){
			int j=0;
			while(j<i){
				if(sz[i]==sz[j]){
					break;
				}
				j++;
			}
			if(j==i){
				String s=sz[i];
				int m=0;
				for(int n=0;n<sz.length;n++){
					if(sz[i]==sz[n]){
						m++;
					}
				}
				hM.put(s, m+"");
			}
			
		}
		System.out.println(hM);
	}
		
}

展开阅读全文

没有更多推荐了,返回首页