从Java字典打印
我想使用字典系统打印文本中每个字符的出现: 文本是“我喜欢苹果” 控制台输出如下所示: “i”在位置上出现2次:1、4 “l”在位置上出现2次:3、11 到目前为止我已经知道了从Java字典打印,java,dictionary,printing,Java,Dictionary,Printing,我想使用字典系统打印文本中每个字符的出现: 文本是“我喜欢苹果” 控制台输出如下所示: “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++) { dictio
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)));
}
String text=“text”;
HashMap dictionary=新建HashMap();
对于(int i=0;i
基本上只是将每个字母添加到字典中的键值中,但我不知道如何打印…我将这样做:
String[] tempArray = data.split("");
IntStream.rangeClosed(1, tempArray.length)
.boxed()
.collect(groupingBy(index -> tempArray[index-1].toUpperCase()))
.forEach((k, v) -> System.out.println(k + " has an occurrence of " +
v.size() + " times on positions " + v.stream()
.map(Object::toString).collect(joining(","))));
- 首先将字符串拆分为表示为字符串的单个字符
- 利用
生成索引IntStream.range
- 使用
按单个字符分组,并将值作为groupingBy
来表示字符出现在其中的索引列表
- 最后,使用
格式化数据并打印到控制台forEach
String data = "I like apples";
这将产生以下输出:
P has an occurrence of 2 times on positions 9,10
has an occurrence of 2 times on positions 2,7
A has an occurrence of 1 times on positions 8
S has an occurrence of 1 times on positions 13
E has an occurrence of 2 times on positions 6,12
I has an occurrence of 2 times on positions 1,4
K has an occurrence of 1 times on positions 5
L has an occurrence of 2 times on positions 3,11
请注意,上述解决方案也将空格视为字符,如果您不希望在输出中使用空格,请使用过滤器
操作将其排除:
IntStream.rangeClosed(1, tempArray.length)
.filter(index -> !tempArray[index-1].trim().isEmpty())
.boxed()
.collect(groupingBy(index -> tempArray[index-1].toUpperCase()))
.forEach((k, v) -> System.out.println(k + " has an occurrence of " +
v.size() + " times on positions " + v.stream()
.map(Object::toString).collect(joining(","))));
此代码使用字典并以要求的精确格式打印正确答案:
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);
}
}
}
你可以试试下面的方法
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GenericPlayAround {
public static void main(String[] args) throws InterruptedException {
String text = "I like apples";
Map<Character, List<Integer>> dictionary = new HashMap<Character, List<Integer>>();
for (int i = 1; i <= text.length(); i++) {
Character c = text.charAt(i-1);
List<Integer> list;
if (dictionary.containsKey(c)) {
list = dictionary.get(c);
}
else {
list = new ArrayList<Integer>();
dictionary.put(c, list);
}
list.add(i);
}
System.out.println(dictionary);
}
}
@我相信这并不是一个真正的重复:另一个问题需要一个使用正则表达式的解决方案——这个问题需要使用字典,也需要位置。不同的问题。@CPerkins当然可以,重新打开。IntStream也像字典一样工作吗?我不明白在这种情况下字典是如何创建的,从我学到的经验中,我读到你可以使用HashMap创建字典,所以我继续使用它,有一个练习要求进行上述操作,但我不知道如何打印。@David
groupingBy
创建了字典(在java中称为Map)。例如,如果您执行了Map result=IntStream.rangeClosed(1,tempArray.length).filter(index->!tempArray[index-1].trim().isEmpty()).boxed().collect(groupingBy(index->tempArray[index-1].toUpperCase())代码>你会有一个元素字典来定义它们的索引。然后你可以迭代它,以你想要的方式格式化它。哦,好的,酷,我需要阅读更多关于IntStream提供的功能。谢谢你的回复!!虽然我需要多练习一些基础知识,因为我正在学习复杂的东西……你需要使用字典吗?是的,我知道如何不用字典来写解决方案,无论如何,这个解决方案更符合我的习惯,我实际上比最初的解法更能理解它。谢谢你的回答!!欢迎您的光临,如果您想对其中任何一个进行解释,我可以在代码中添加注释,这也会很有帮助!虽然我有点明白这是怎么回事,主要是。。。除息的
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class GenericPlayAround {
public static void main(String[] args) throws InterruptedException {
String text = "I like apples";
Map<Character, List<Integer>> dictionary = new HashMap<Character, List<Integer>>();
for (int i = 1; i <= text.length(); i++) {
Character c = text.charAt(i-1);
List<Integer> list;
if (dictionary.containsKey(c)) {
list = dictionary.get(c);
}
else {
list = new ArrayList<Integer>();
dictionary.put(c, list);
}
list.add(i);
}
System.out.println(dictionary);
}
}
{ =[2, 7], p=[9, 10], a=[8], s=[13], e=[6, 12], I=[1], i=[4], k=[5], l=[3, 11]}