java 中文符号占位_java – ‘占位符’字符以避免积极比较?
我正在研究CodingBat exercises for Java.我遇到了以下問題:
Given 2 arrays that are the same length containing strings, compare the 1st string in one array to the 1st string in the other array, the 2nd to the 2nd and so on. Count the number of times that the 2 strings are non-empty and start with the same char. The strings may be any length, including 0.
我的代碼是這樣的:
public int matchUp(String[] a, String[] b){
int count = 0;
for (int i = 0; i < a.length; i++) {
String firstLetterA = a[i].length() == 0
? "ê"
: a[i].substring(0, 1);
String firstLetterB = b[i].length() == 0
? "é"
: b[i].substring(0, 1);
if (firstLetterA.equals(firstLetterB)) {
count++;
}
}
return count;
}
我的問題是:哪個“占位符”字符被認為是一種良好的做法,可以避免在firstLetterA和firstLetterB之間進行不必要的比較?
在這種情況下,我只分配了兩個很少使用的不同字母(至少用英文).我嘗試使用”(一個空字符,而不是空格)但當然,它們相互匹配.我也嘗試過使用null,因為我認為它無法進行正面比較,但這也會導致問題.
總結
以上是生活随笔為你收集整理的java 中文符号占位_java – ‘占位符’字符以避免积极比较?的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 办一张新的移动的电话卡多少钱?
- 下一篇: 在拍婚纱照多少钱啊?
