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,因为我认为它无法进行正面比较,但这也会导致问题.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值