`
a20071426
  • 浏览: 24374 次
  • 性别: Icon_minigender_1
  • 来自: 北京
最近访客 更多访客>>
社区版块
存档分类
最新评论

【一道java题】识别出相同的组

阅读更多

String txet=“abc ac a abc a  cd ef e”

 

要求输出:abc  2

                 ac  1

                  a   2

 

我只是用容器中的HashMap做了一下,输出的结果形式上不一样,可能还有其他方法能正确实现。

 

package Map;

/**
 * 探讨一道题目,使用容器中的HashMap做出来,比较有参考意义
 * 
 */
import java.util.*;

public class HashMapTest {

	public static final int ONE = 1;

	public static final String text = "abc ac a ab cd  cd ef e";

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		// split 对字符串进行截取,截取符合自己要求的子字符串,并将子字符串放入数组中
		String[] t = text.split(" ");
		Map m = new HashMap();

		for (int i = 0; i < t.length; i++) {

			System.out.print(t[i] + " ");
		}
		System.out.print("\n");
		System.out.println(t.length);

		for (int i = 0; i < t.length; i++) {

			int freq = (Integer) m.get(t[i]) == null ? ONE : (Integer) m
					.get(t[i]) + 1;
			m.put(t[i], freq);
		}

		System.out.println("HashMap 中共有" + m.size() + "对元素!");

		System.out.print(m);
	}

}
 

 

 

 

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics