我想使用字典系统在文本中打印每个字符的出现情况:
例如:文字是“我喜欢苹果”
控制台输出如下:
“ i”在以下位置出现了2次:1、4
'l'在位置上出现2次:3,11
..
到目前为止,我已经知道了
String text = "text";
HashMap<Integer, String> dictionary = new HashMap<Integer, String>();
for (int i = 0; i < text.length(); i++) {
dictionary.put(i, String.valueOf(text.charAt(i)));
}
基本上只是将每个字母添加到字典中的键值上,但是我不知道如何进行打印...
参考方案
此代码使用字典,并以要求的确切格式打印正确答案:
import java.util.ArrayList;
import java.util.HashMap;
public class TestDriver {
public static void main(String[] args) {
String text = "i like apples";
char[] textArray = text.toCharArray();
//a dictionary that will hold the letter as the key and a list of it's positions as the value.
HashMap<Character, ArrayList<Integer>> dictionary = new HashMap<Character, ArrayList<Integer>>();
//loop through the text to check each letter
for (int i = 0; i < textArray.length; i++) {
//if you've already checked this letter, skip to the next one
if(dictionary.containsKey(textArray[i])) {
continue;
}
//add the letter's position to its position list
ArrayList<Integer> positionList = new ArrayList<>();
positionList.add(i);
//compare the remaining letters in the text to the current letter
for (int j = i+1; j < textArray.length; j++) {
//if a letter matches, add it's position to the list
if(textArray[i] == textArray[j]) {
positionList.add(j);
}
}
//add the letter and its list of positions to the dictionary
dictionary.put(textArray[i], positionList);
}
//format the response
for(char key : dictionary.keySet()) {
ArrayList<Integer> positions = new ArrayList<>();
positions = dictionary.get(key);
System.out.println(key + " has an occurance of " + positions.size() + " on positions: " + positions);
}
}
}
打印到控制台:
has an occurance of 2 on positions: [1, 6]
p has an occurance of 2 on positions: [8, 9]
a has an occurance of 1 on positions: [7]
s has an occurance of 1 on positions: [12]
e has an occurance of 2 on positions: [5, 11]
i has an occurance of 2 on positions: [0, 3]
k has an occurance of 1 on positions: [4]
l has an occurance of 2 on positions: [2, 10]
Java中的“ <<”运算符 - java最喜欢的语句来自Java的Character类:(1 << Character.PARAGRAPH_SEPARATOR)) >> type PARAGRAPH_SEPARATOR是字节,type是整数。这句话中的操作员,他们做什么?如何以及在哪里可以使用这些运算符?这是oracles java.lang.Character文档。该类中…
JAVA:字节码和二进制有什么区别? - javajava字节代码(已编译的语言,也称为目标代码)与机器代码(当前计算机的本机代码)之间有什么区别?我读过一些书,他们将字节码称为二进制指令,但我不知道为什么。 参考方案 字节码是独立于平台的,在Windows中运行的编译器编译的字节码仍将在linux / unix / mac中运行。机器代码是特定于平台的,如果在Windows x86中编译,则它将仅在Win…
java:继承 - java有哪些替代继承的方法? java大神给出的解决方案 有效的Java:偏重于继承而不是继承。 (这实际上也来自“四人帮”)。他提出的理由是,如果扩展类未明确设计为继承,则继承会引起很多不正常的副作用。例如,对super.someMethod()的任何调用都可以引导您通过未知代码的意外路径。取而代之的是,持有对本来应该扩展的类的引用,然后委托给它。这是与Eric…
Java:BigInteger,如何通过OutputStream编写它 - java我想将BigInteger写入文件。做这个的最好方式是什么。当然,我想从输入流中读取(使用程序,而不是人工)。我必须使用ObjectOutputStream还是有更好的方法?目的是使用尽可能少的字节。谢谢马丁 参考方案 Java序列化(ObjectOutputStream / ObjectInputStream)是将对象序列化为八位字节序列的一种通用方法。但…
Java-如何将此字符串转换为日期? - java我从服务器收到此消息,我不明白T和Z的含义,2012-08-24T09:59:59Z将此字符串转换为Date对象的正确SimpleDateFormat模式是什么? java大神给出的解决方案 这是ISO 8601标准。您可以使用SimpleDateFormat simpleFormat = new SimpleDateFormat("yyyy-MM…