[英]How to count the number of a user entered character in an array?
我有一個 Java 分配,我需要讀取一個字符,然后計算該字符在數組中出現的次數。 這就是我到目前為止所擁有的。
import javax.swing.JOptionPane;
public class ArraySring
{
public static void main(String args[])
{
String userChar;
userChar = JOptionPane.showInputDialog("Enter a character");
String dow[] = {
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
};
for(int x=0; x<7; x++) {
System.out.println(dow[x]);
}
System.out.println();
}
} // end ArrayStrings
char
避免使用char
類型,因為它現在是legacy 。 該類型是在Unicode擴展超出其原始64K 限制之前發明的。 Unicode 現在定義了兩倍多的字符:Unicode 中的 143,859 個字符 13. 因此,當遇到超出基本多語言平面的字符(大約前 64,000 個字符)時,使用char
將失敗。
在現代 Java 中,我們應該使用代碼點編號而不是char
值。
例如,讓我們假設在拼寫某些星期幾的名稱時使用了表情符號字符FACE WITH MEDICAL MASK 。 愚蠢,但適合演示。 Unicode 中的該字符分配給代碼點 128,567。
此代碼使用IntStream
。流是在 Java 中表示一系列值的另一種方式。 作為初學者學習 Java 現在對你來說並不重要。 只要知道我們可以通過調用string.codePoints().toArray()
從 stream 提供的一系列值中創建一個數組。 我們最終得到一個int
integer 數字數組。 每個int
integer 數字是String
文本中字符的代碼點。
String userCharacter = "😷";
String strings[] = { "M😷nday" , "Tuesd😷y" , "Wednesday" };
int count = 0;
for ( String string : strings ) // Loop through the day-of-week names.
{
IntStream codePoints = string.codePoints(); // Get a stream of `int` integer numbers, the Unicode code point number for each character in the string.
int[] ints = codePoints.toArray(); // Convert stream to an array.
for ( int i : ints ) // Loop through each number, each code point.
{
System.out.println( "i = " + i + " character: " + Character.toString( i ) ); // Debugging. Dump values to the console.
if ( i == userCharacter.codePointAt( 0 ) ) // Compare this nth number (the code point of the nth character in the day-of-week name) against the code point number of the character given by the user.
{
System.out.println( "Hit.") ; // Debugging.
count++; // If they match, increment our counter.
}
}
}
轉儲到控制台。
System.out.println( "count = " + count ); // Dump to console.
System.out.println( "userCharacter.codePointAt( 0 ): " + userCharacter.codePointAt( 0 ) );
跑的時候。
i = 77 character: M
i = 128567 character: 😷
Hit.
i = 110 character: n
i = 100 character: d
i = 97 character: a
i = 121 character: y
i = 84 character: T
i = 117 character: u
i = 101 character: e
i = 115 character: s
i = 100 character: d
i = 128567 character: 😷
Hit.
i = 121 character: y
i = 87 character: W
i = 101 character: e
i = 100 character: d
i = 110 character: n
i = 101 character: e
i = 115 character: s
i = 100 character: d
i = 97 character: a
i = 121 character: y
count = 2
userCharacter.codePointAt( 0 ): 128567
有關更多信息,請參閱:
這里是 go:
public static void main(String args[]) {
char userChar = JOptionPane.showInputDialog("Enter a character").toLowerCase().charAt(0);
int count = 0;
String dow[] = // ...
for (String str : dow)
for (char ch : str.toLowerCase().toCharArray())
if (ch == userChar)
count++;
System.out.println(count);
}
巴茲爾的回應很好,感謝您的全面描述。 接受 Basil 關於使用代碼點而不是字符的建議,除此之外,使用一些 stream 處理也將解決方案轉換為:
public static void main(String args[]) {
int inputCodePoint = JOptionPane.showInputDialog("Enter a character").toLowerCase().codePointAt(0);
String dow[] = {
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday",
"Sunday"
};
System.out.println(Arrays.stream(dow)
.flatMap(s -> Arrays.stream(s.split("")))
.filter(s -> s.equals(Character.toString(inputCodePoint)))
.count());
}
這利用了JDK 11中添加的Character.toString(int) 。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.