Given ["a", "b", "a", "b", "c", "a", "b", "c", "b"] string array, then use the Map key to save The string element of the array, value holds the number of occurrences of the string element.

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);
	}
		
}

Published 26 original articles · praised 1 · visits 9978

Guess you like

Origin blog.csdn.net/qq_31884013/article/details/49682393