'Placeholder' 避免正面比较的字符?
'Placeholder' character to avoid positive comparison?
我正在完成 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;
}
我的问题是:使用哪个 'placeholder' 字符可以避免 firstLetterA
和 firstLetterB
之间不必要的比较?
在这种情况下,我只是分配了两个很少使用(至少在英语中)的不同字母。我尝试只使用 ''
(一个空字符,而不是 space),但当然,它们相互匹配。我也尝试对两者都使用 null
,因为我认为它不能进行正面比较,但这也会导致问题。
在我看来,一个好的做法是扩展 if
条件并且根本不使用任何虚拟字符:
for (int i = 0; i < a.length; i++) {
if (!a[i].isEmpty() && !b[i].isEmpty() && a[i].charAt(0) == b[i].charAt(0)) {
count++;
}
}
这是替代解决方案。
- 为每个数组迭代嵌套的二维循环
- 检查两个数组中的字符是否具有相同的顺序和
字符串长度
- 通过子字符串方法比较字符。
- 增加计数
。
public static int matchingChar(String[] a, String[] b) {
int count=0;
for(int i=0;i<a.length;i++) {
for(int j=0;j<b.length;j++) {
if(i==j && a[i].length()!=0 && b[i].length()!=0) {
if(a[i].startsWith(b[i].substring(0,1))) {
count++;
}
}
}
}
return count;
}
希望对您有所帮助
public int matchUp(String[] a, String[] b) {
int count = 0;
for(int i=0;i<a.length;i++)
{
if(a[i].equals("") || b[i].equals(""))
continue;
if(a[i].charAt(0) == b[i].charAt(0))
count++;
}
return count;
}
当前数组元素为空时继续下一个元素
我正在完成 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;
}
我的问题是:使用哪个 'placeholder' 字符可以避免 firstLetterA
和 firstLetterB
之间不必要的比较?
在这种情况下,我只是分配了两个很少使用(至少在英语中)的不同字母。我尝试只使用 ''
(一个空字符,而不是 space),但当然,它们相互匹配。我也尝试对两者都使用 null
,因为我认为它不能进行正面比较,但这也会导致问题。
在我看来,一个好的做法是扩展 if
条件并且根本不使用任何虚拟字符:
for (int i = 0; i < a.length; i++) {
if (!a[i].isEmpty() && !b[i].isEmpty() && a[i].charAt(0) == b[i].charAt(0)) {
count++;
}
}
这是替代解决方案。
- 为每个数组迭代嵌套的二维循环
- 检查两个数组中的字符是否具有相同的顺序和 字符串长度
- 通过子字符串方法比较字符。
- 增加计数
。
public static int matchingChar(String[] a, String[] b) {
int count=0;
for(int i=0;i<a.length;i++) {
for(int j=0;j<b.length;j++) {
if(i==j && a[i].length()!=0 && b[i].length()!=0) {
if(a[i].startsWith(b[i].substring(0,1))) {
count++;
}
}
}
}
return count;
}
希望对您有所帮助
public int matchUp(String[] a, String[] b) {
int count = 0;
for(int i=0;i<a.length;i++)
{
if(a[i].equals("") || b[i].equals(""))
continue;
if(a[i].charAt(0) == b[i].charAt(0))
count++;
}
return count;
}
当前数组元素为空时继续下一个元素