介绍
在 Java 中,我们可以轻松地计算字符串中特定字符出现的次数。本实验将引导你了解可用于计算字符串中字符出现次数的不同方法。
在 Java 中,我们可以轻松地计算字符串中特定字符出现的次数。本实验将引导你了解可用于计算字符串中字符出现次数的不同方法。
我们可以使用迭代方法遍历字符串中的每个字符,以计算特定字符出现的次数。
在 ~/project
目录下创建一个名为 CountOccurrences.java
的 Java 文件,并将以下内容复制到文件中:
public class CountOccurrences {
public static int countChars(String str, char c) {
int count = 0;
for(int i = 0; i < str.length(); i++) {
char currChar = str.charAt(i);
if(currChar == c)
count += 1;
}
return count;
}
public static void main(String[] args) {
String s = "Java is an awesome language!";
int charCountOfA = countChars(s, 'a');
int charCountOfG = countChars(s, 'g');
int charCountOfE = countChars(s, 'e');
System.out.println("The String is: " + s);
System.out.println("Character count of 'a': " + charCountOfA);
System.out.println("Character count of 'g': " + charCountOfG);
System.out.println("Character count of 'e': " + charCountOfE);
}
}
要编译并运行程序,请在终端中运行以下命令:
cd ~/project
javac CountOccurrences.java && java CountOccurrences
输出结果应为:
The String is: Java is an awesome language!
Character count of 'a': 6
Character count of 'g': 2
Character count of 'e': 3
我们也可以使用递归方法来计算字符串中字符的出现次数。这涉及使用两个方法,第一个方法是递归的,第二个方法调用第一个方法。
将以下代码块复制并粘贴到 CountOccurrences.java
文件中 Step 1 的代码块之后:
public static int countCharsRecur(String str, char c, int idx) {
if(idx >= str.length())
return 0;
else {
int count = 0;
if(str.charAt(idx) == c)
count = 1;
return count + countCharsRecur(str, c, idx + 1);
}
}
public static int countChars(String s, char c) {
return countCharsRecur(s, c, 0);
}
要编译并运行更新后的程序,请运行与 Step 1 中相同的命令:
cd ~/project
javac CountOccurrences.java && java CountOccurrences
我们可以使用 Java 8 Streams 来计算字符串中字符的出现次数。
将以下代码块复制并粘贴到 CountOccurrences.java
文件中 Step 2 的代码块之后:
public static void usingStreams(String s) {
int charCountOfA = (int) s.chars().filter(c -> c == 'a').count();
int charCountOfG = (int) s.chars().filter(c -> c == 'g').count();
int charCountOfE = (int) s.chars().filter(c -> c == 'e').count();
System.out.println("The String is: " + s);
System.out.println("Character count of 'a': " + charCountOfA);
System.out.println("Character count of 'g': " + charCountOfG);
System.out.println("Character count of 'e': " + charCountOfE);
}
要使用 Java 8 Streams 方法,请在 main
方法中的 Step 2 代码块之后添加以下代码行:
usingStreams(s);
要编译并运行更新后的程序,请运行与 Step 1 中相同的命令:
cd ~/project
javac CountOccurrences.java && java CountOccurrences
我们可以使用外部库来计算字符串中字符的出现次数。
Guava 库提供了 CharMatcher
类,可以计算给定字符的出现次数。
在 Step 3 的代码块之后添加以下代码块:
public static int usingGuava(String str) {
CharMatcher cm = CharMatcher.is('a');
int charCountOfA = cm.countIn(str);
System.out.println("Character count of 'a': " + charCountOfA);
return charCountOfA;
}
要使用 Guava 库,请在 CountOccurrences.java
文件的顶部添加以下导入:
import com.google.common.base.CharMatcher;
在 main
方法中,使用字符串参数调用 usingGuava
方法:
usingGuava(s);
要编译并运行更新后的程序,请运行与 Step 1 中相同的命令:
cd ~/project
javac -cp ".:./lib/*" CountOccurrences.java && java -cp ".:./lib/*" CountOccurrences
Apache Commons 库提供了一个 StringUtils
类,其中包含一个方便的 countMatches()
方法,该方法接受一个字符和一个字符串作为输入,并返回该字符在字符串中的出现次数。
在 Step 4 的代码块之后添加以下代码块:
public static int usingApache(String str) {
int charCountOfA = StringUtils.countMatches(str, 'a');
int charCountOfG = StringUtils.countMatches(str, 'g');
int charCountOfE = StringUtils.countMatches(str, 'e');
System.out.println("Character count of 'a': " + charCountOfA);
System.out.println("Character count of 'g': " + charCountOfG);
System.out.println("Character count of 'e': " + charCountOfE);
return charCountOfA;
}
要使用 Apache Commons 库,请在 CountOccurrences.java
文件的顶部添加以下导入:
import org.apache.commons.lang3.StringUtils;
在 main
方法中,使用字符串参数调用 usingApache
方法:
usingApache(s);
要编译并运行更新后的程序,请运行与 Step 1 中相同的命令,并添加类路径:
cd ~/project
javac -cp ".:./lib/*" CountOccurrences.java && java -cp ".:./lib/*" CountOccurrences
在本实验中,我们涵盖了以下步骤:
完成本实验后,你应该对计算字符串中字符出现次数的不同方法有更好的理解。