<code id="ymukc"><xmp id="ymukc">


CarinAcosta的頭像 CarinAcosta 0 2016-03-02 19:48 1


× 1   

瀏覽數: 22123

分享時間: 2 年 前

import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
 * 統計abacbacdadbc中的每個字母出現的次數,輸出格式是:a(4)b(3)c(3)d(2)
 * 選中TreeMap的原因是:key不重復且按順序排序取出
 * 思路:
 * 1.將字符串abacbacdadbc轉換成字符數組
 * 2.分別取字符,與TreeMap中的key進行比較
 *   2.1 如果TreeMap中存在對應的字符,則取出并自增,再存入TreeMap
 *   2.2 如果TreeMap中不存在對應的字符,則直接存入該字符,value=1

public class A3B1C2_TreeMapTest {

	public static void main(String[] args) {

		TreeMap<String, Integer> tm=new TreeMap<>();
		String string="abacbacdadbc";
		char[] ch=string.toCharArray();
		for(char r :ch){
			//tm.put(String.valueOf(r), 1);
			if(compareKey(r, tm))
				int i=tm.get(String.valueOf(r));
				tm.put(String.valueOf(r), i);
			}else {
				tm.put(String.valueOf(r), 1);
		Set<Map.Entry<String, Integer>> entrySet=tm.entrySet();
		Iterator<Map.Entry<String, Integer>> iterator=entrySet.iterator();
			Map.Entry<String, Integer> me=iterator.next();
			String key=me.getKey();
			int value=me.getValue();
	public static boolean compareKey(char c,TreeMap<String, Integer> treeMap){
		if (treeMap.containsKey(String.valueOf(c))) {
			return true;
		return false;


  • ossaa的頭像 ossaa 2018-09-06 23:03 代碼數:0

    Wonderful illustrated information. I thank you about that. No doubt it will be very useful for my future projects. Would like to see some other posts on the same subject! EMS Ger?te

  • cixexey 的頭像 cixexey 2018-09-17 20:56 代碼數:0

    I really appreciate this wonderful post that you have provided for us. I assure this would be beneficial for most of the people.tech tips

  • pevahanifo的頭像 pevahanifo 2018-09-22 19:20 代碼數:0

    Very interesting blog. Alot of blogs I see these days don't really provide anything that I'm interested in, but I'm most definately interested in this one. Just thought that I would post and let you know. technology updates

  • banjue的頭像 banjue 2017-04-28 16:50 代碼數:0


  • viable的頭像 viable 2017-05-19 17:16 代碼數:0

    太麻煩了.    value 用  三元 運算符 就可以了

  • ossaa的頭像 ossaa 2018-08-12 16:31 代碼數:0

    I would like to thank you for the efforts you have made in writing this article. I am hoping the same best work from you in the future as well. Thanks...Onlinr Quran lessons

  • cixexey的頭像 cixexey 2018-08-20 15:29 代碼數:0

    I felt very happy while reading this site. This was really very informative site for me. I really liked it. This was really a cordial post. Thanks a lot!. search engine marketing

  • cixexey的頭像 cixexey 2018-08-21 20:02 代碼數:0

    The website is looking bit flashy and it catches the visitors eyes. Design is pretty simple and a good user friendly interface.Just Cannabis

  • cixexey的頭像 cixexey 2018-08-24 02:05 代碼數:0

    this is really nice to read..informative post is very good to read..thanks a lot! jual mobil bekas

  • cixexey的頭像 cixexey 2018-08-24 19:16 代碼數:0

    Thanks for the blog loaded with so many information. Stopping by your blog helped me to get what I was looking for.Lawyer


<code id="ymukc"><xmp id="ymukc">
<code id="ymukc"><xmp id="ymukc">