Educoder–Java高级特性(第三章)- Java 字符串与集合练习——词频统计【笔记+参考代码】

Educoder–Java高级特性(第三章)- Java 字符串与集合练习——词频统计【笔记+参考代码】


第一关


编程要求
请仔细阅读右侧代码,根据方法内的提示,在Begin - End区域内进行代码补充,具体任务如下:

用String.split()方法将字符串“aaa|bbb|ccc”以“|”进行拆分,用StringTokenizer类将字符串“This?is?a?test?string”以“?”进行拆分。
测试说明 补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。


测试输入:

aaa|bbb|ccc

This?is?a?test?string

预期输出:

aaa

bbb

ccc

This

is

a

test

string


提示:

“|”、“.”、“*”、“+”、“\”等不是有效的模式匹配规则表达式,是转义字符,使用split()方法时必须得加"\"才行。



参考代码

package step1;
import java.util.List;
import java.util.ArrayList;
import java.util.StringTokenizer;
public class StudentDemo{

	//使用String.split()方法分割
	public List<String> splitPartition(String str){
		List<String> list=new ArrayList<String>();
					
		//请在此添加实现代码
		/********** Begin **********/
		String[] strArray=str.split("\\|");
		for(String str2:strArray){
			System.out.println(str2);
		}
		System.out.println();
		/********** End **********/
		return list;
	}

	//使用StringTokenizer类进行分割
	public List<String> tokenPartition(String str){
		List<String> list=new ArrayList<String>();
		//请在此添加实现代码
		/********** Begin **********/
		String[] strArray=str.split("\\?");
		for(String str1:strArray){
			System.out.println(str1);
		}
		/********** End **********/
		return list;
	}
}



第二关


编程要求
请仔细阅读右侧代码,根据方法内的提示,在Begin - End区域内进行代码补充,具体任务如下:

列出给定字符串中每个单词(按“ ”,“,”,“?”,“.”,“!”,“:”,“\n”分割)首次出现的位置。 测试说明
补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。


测试输入:

No arguments will give courage to the coward.

预期输出:

单词:the—首次出现的角标34

单词:No—首次出现的角标0

单词:give—首次出现的角标18

单词:will—首次出现的角标13

单词:arguments—首次出现的角标3

单词:to—首次出现的角标31

单词:coward—首次出现的角标38

单词:courage—首次出现的角标23


提示:

//1.分割单词时可一次进行 //2.可以采用Map集合的键值对存储
Map<String, Integer> map = newHashMap<String, Integer>(); map.put(“Hello”,0); map.put(“world”,1);
Set<Entry<String, Integer>> entrySet = wordCount.entrySet();
for(Entry<String, Integer> entry : entrySet) {
System.out.println(entry.getKey()+"—"+entry.getValue());
}
输出:

Hello—0

world—1

这一关做得本人十分迷惑,不是很明白输出是按什么规则来的,而且在测试平台给出的参考答案也跟没有分割一样,过不了测评,在这里给平台的参考答案,如果有人过了这关欢迎在评论区交流一下,谢谢~

package step2;
import java.util.Map;
import java.util.HashMap;
import java.util.StringTokenizer;
public class StudentDemo{
	//返回一个Map集合来得到单词和首次出现的下标  key为单词名称  value为单词的角标
	public Map<String, Integer> getMap(String str){
		Map<String, Integer> map = new HashMap<String, Integer>();
		//对str进行分割   再加入map集合中
		//请在此添加实现代码
		/********** Begin **********/
		StringTokenizer tok = new StringTokenizer(str);
		String word;
		while(tok.hasMoreTokens()){
			word = tok.nextToken(",.\n");
			int index = str.indexOf(word);
			map.put(word,index);
		}
		/********** End **********/
		return map;
	}
}

在这里插入图片描述



第三关


编程要求
请仔细阅读右侧代码,根据方法内的提示,在Begin - End区域内进行代码补充,具体任务如下:

将指定文本(可以通过右侧文件目录下的src/step3/readme.txt查看)以降序的方式输出每个单词出现的次数。
测试说明
补充完代码后,点击测评,平台会对你编写的代码进行测试,当你的结果与预期输出一致时,即为通过。

预期输出:
you-16
to-10
have-6
want-5
the-5
who-5
and-5
of-5
a-4
those-4
make-4
go-4
enough-4
in-3
be-3
that-3
for-3
with-3
your-2
they-2
past-2
them-2
it-2
The-2
everything-2
one-2
people-2
do-2
only-2
hurts-2
their-2
life-2
just-2
on-2
what-2
don’t-1
happy-1
sorrow-1
heartaches-1
hope-1
kiss-1
when-1
put-1
Dream-1
begins-1
lifeuntil-1
tried-1
appreciate-1
pick-1
let-1
because-1
grows-1
based-1
lives-1
Always-1
If-1
much-1
hurt-1
strong-1
other-1
touched-1
May-1
probably-1
comes-1
tear-1
are-1
too-1
where-1
so-1
can’t-1
Happiness-1
failures-1
shoes-1
trials-1
future-1
sweet-1
happiest-1
feel-1
smile-1
There-1
things-1
forgotten-1
from-1
yourself-1
others-1
all-1
always-1
real-1
most-1
searched-1
dream-1
person-1
Love-1
keep-1
importance-1
dreams-1
best-1
necessarily-1
miss-1
can-1
moments-1
along-1
happiness-1
brightest-1
ends-1
lies-1
human-1
chance-1
will-1
cry-1
way-1
someone-1
well-1
hug-1

同样迷惑的一关,给出的答案好像没有用到升降序排序输出啊,但是却可以过测评???

package step3;
import java.util.Map;
import java.util.HashMap;
import java.util.StringTokenizer;
public class StudentDemo{
	//获取单词的数量
	public Map<String, Integer> getWordCount(String str) {
		Map<String, Integer> map = new HashMap<String, Integer>();
		
		//请在此添加实现代码
		/********** Begin **********/
		StringTokenizer tokenizer = new StringTokenizer(new String(str));
    	int count;
		String word;
		while (tokenizer.hasMoreTokens()) {
			word = tokenizer.nextToken(" ,?.!:;\"\"‘’\n");
			if (map.containsKey(word)) {
				count = map.get(word);
				map.put(word, count + 1);
			} else {
				map.put(word, 1);
			}
		}
		/********** End **********/
		return map;
	}
}

【笔记】entrySet的用法

public static void main(String[] args) {


  Map<String, String> map = new HashMap<String, String>();
  map.put("1", "value1");
  map.put("2", "value2");
  map.put("3", "value3");
  
  //第一种:普遍使用,二次取值
  System.out.println("通过Map.keySet遍历key和value:");
  for (String key : map.keySet()) {
   System.out.println("key= "+ key + " and value= " + map.get(key));
  }
  
  //第二种
  System.out.println("通过Map.entrySet使用iterator遍历key和value:");
  Iterator<Map.Entry<String, String>> it = map.entrySet().iterator();
  while (it.hasNext()) {
   Map.Entry<String, String> entry = it.next();
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }
  
  //第三种:推荐,尤其是容量大时
  System.out.println("通过Map.entrySet遍历key和value");
  for (Map.Entry<String, String> entry : map.entrySet()) {
   System.out.println("key= " + entry.getKey() + " and value= " + entry.getValue());
  }

  //第四种
  System.out.println("通过Map.values()遍历所有的value,但不能遍历key");
  for (String v : map.values()) {
   System.out.println("value= " + v);
  }
 }

猜你喜欢

转载自blog.csdn.net/weixin_44177494/article/details/104976774