[英]Find the occurrence of each character in a given string
“我想查找並打印給定字符串的每個字符的出現,並且我已經建立了自己的邏輯,但存在一些問題。例如,如果我將輸入輸入為'JAVA'<\/strong> 。我的程序產生的輸出將是<\/em>
J 1
A 2
V 1
A 1
代碼打印方式的原因是,循環將打印給定索引的每個字符(以及后續的匹配項)。 您確實需要用一個循環將字符和計數存儲在數據結構中,然后用一秒鍾顯示計數。 LinkedHashMap<Character, Integer>
非常適合您的用例(因為它保留了鍵插入順序,因此不需要其他邏輯來還原輸入順序)。 我將進行的其他更改包括使用String.toCharArray()
和for-each
循環 。 喜歡,
Map<Character, Integer> map = new LinkedHashMap<>();
for (char ch : x.toUpperCase().toCharArray()) {
map.put(ch, map.getOrDefault(ch, 0) + 1);
}
for (char ch : map.keySet()) {
System.out.printf("%c\t%d%n", ch, map.get(ch));
}
我用x
等於JAVA
進行測試並得到了(按要求)
J 1
A 2
V 1
這不是一個最佳的解決方案,但是我嘗試了盡可能少地更改您的代碼 :
public static void main(String args[]) {
Scanner input = new Scanner(System.in);
System.out.println("Enter a string");
// use a StringBuilder to delete chars later on
StringBuilder x = new StringBuilder(input.nextLine().toUpperCase());
for(int i=0;i<x.length();i++) {
int count=1;
char find = x.charAt(i);
// go through the rest of the string from the end so we do not mess up with the index
for(int j=x.length()-1;j>i;j--) {
if(find == x.charAt(j)) {
count++;
// delete counted occurences of the same char
x.deleteCharAt(j);
}
}
System.out.printf("%c\t%d",x.charAt(i),count);
System.out.println();
}
}
我更喜歡的 Java流如下所示:
input.nextLine().toUpperCase().chars()
.mapToObj(i -> (char) i)
.collect(Collectors.groupingBy(Function.identity(), LinkedHashMap::new, Collectors.counting()))
.forEach((k, v) -> System.out.println(k + "\t" + v));
使用hashMap可以很容易地累積出現次數,並且可以輕松地打印迭代的HashMap。
這是代碼:
public class FindOccuranceOfCharacter {
public static void main(String[] args) {
String x;
Scanner input = new Scanner(System.in);
System.out.println("Enter a string");
x = input.nextLine();
HashMap<Character,Integer> occurance = new HashMap<Character,Integer>();
x = x.toUpperCase();
int size = x.length();
for(int i =0;i<size;i++) {
int count=1;
char find = x.charAt(i);
occurance.put(find, occurance.getOrDefault(find, 0) + 1);
}
for (Character key : occurance.keySet()) {
Integer value = occurance.get(key);
System.out.println("Key = " + key + ", Value = " + value);
}
}
/* Most common string occurrence related solutions using java 8 */
//find all character occurrences in a string
String myString = "test";
List<Character> list = myString.chars().mapToObj(c -> (char)c).collect(Collectors.toList());
list.stream().distinct().forEach(c -> System.out.println(c + " = " + Collections.frequency(list, c)));
//find one specific character occurrence in a string
String myString = "test";
char search = 't';
long count = myString.chars().filter(c -> c == search).count();
System.out.println(count);
//find all unique characters in a string
String myString = "test";
List<Character> list = myString.chars().mapToObj(c -> (char)c).collect(Collectors.toList());
list.stream().filter(c -> Collections.frequency(list,c) == 1).forEach(System.out::println);
//find first unique character in a string
String myString = "test";
List<Character> list = myString.chars().mapToObj(c -> (char)c).collect(Collectors.toList());
char firstUniqueChar = list.stream().filter(c -> Collections.frequency(list,c) == 1).findFirst().get();
System.out.println(firstUniqueChar);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.