免责声明:这是一个家庭作业问题。我正在尝试编写一个contains(java.lang.String subString)
方法,该方法为定制的String类返回一个int
值,该值表示主字符串中比较字符串的索引。
一些规则:
length()
返回主字符串的长度(正是它的作用)我的代码:
public int contains(java.lang.String subString) {
this.subString = subString;
char[] arrSubStr = this.subString.toCharArray();
//Create initial fail
int index = -1;
//Make sure comparison subString is the same length or shorter than the primary string
if(arrSubStr.length > length()) {
return index;
}
//Steps to perform if initial conditions are met
else {
//Compare first character of subString to each character in primary string
for(int i = 0; i < length(); i++) {
//When a match is found...
if(arrSubStr[0] == this.content[i]) {
//...make sure that the subString is not longer than the remaining length of the primary string
if(arrSubStr.length > length() - i) {
return index;
}
//Proceed matching remainder of subString
else {
//Record the index of the beginning of the subString contained in primary string
index = i;
//Starting with second character of subString...
for(int j = 1; j < arrSubStr.length;) {
//...compare with subsequent chars of primary string,
//and if a failure of match is found, reset index to failure (-1)
if(arrSubStr[j] != this.content[j+i]) {
index = -1;
return index;
}
//If we get here, it means whole subString match found
//Return the index (=i) we set earlier
else {
return index;
}
}
}
}
}
}
return index;
}
测试结果:
主字符串:asdfg
比较字符串:donkey
结果:-1
[通过]
主字符串:asdfg
比较字符串:asdfg
结果:0
[通过]
主字符串:asdfg
比较字符串:g
结果:4
[通过]
主字符串:asasasf
比较字符串:asd
结果:0
[失败](应为-1
)
主字符串:asasasf
比较字符串:asf
结果:0
[失败](应为4
)
这些注释反映了代码的预期工作方式。但是很明显,当它到达第二个for
循环时,逻辑将以某种方式分解以给出上面的结果。但是我看不到问题。我可以再看看吗?
答案 0 :(得分:1)
//If we get here, it means whole subString match found
//Return the index (=i) we set earlier
else {
return index;
}
不幸的是,这种假设是不正确的。如果到达那里,则意味着两个子字符串的第二个字符是相同的,因为if-else
语句将只执行一次,并且两端都包含一个return
。
既然我已经诊断出问题,但解决此问题的方法可能很容易,但我想对此做进一步说明。我们每天尝试编写代码的方式是一种使我们使用的代码可维护,可重用和可测试的方式。
基本上,这意味着我们可以轻松地将我们在这里拥有的功能分解为一个又一个又一个的小功能,我们可以针对这些小功能编写单元测试,并获得关于一组逻辑语句是否合适的快速反馈。 / p>
答案 1 :(得分:1)
在评论中引用了Jai和azurefrog的建议,我能够通过将逻辑重写为以下内容(略有删节)来解决问题:
if(arrSubStr.length > length()) {
return index;
}
//Steps to perform if initial conditions are met
else {
//Compare first character of subString to each character in primary string
for(int i = 0; i < length(); i++) {
//When a match is found...
if(arrSubStr[0] == this.content[i]) {
//...make sure that the subString is not longer than the remaining length of the primary string
if(arrSubStr.length <= length() - i) {
//Record the index of the beginning of the subString contained in primary string
index = i;
//Starting with second character of subString...
for(int j = 1; j < arrSubStr.length; j++) {
//...compare with subsequent chars of primary string,
//and if a failure of match is found, reset index to failure (-1)
if(arrSubStr[j] != this.content[j+i]) {
index = -1;
break;
}
}
}
}
}
}
return index;
从本质上讲,我从循环内删除了所有return
语句。事后看来,简单地适当设置index
值并利用最终的{外部} return
语句是解决问题的正确方法。然后,我还向内部break;
循环中添加了for
,以确保不匹配将继续循环运行。我确定其中仍然有不必要的代码,但是尽管它仍然通过了必要的测试,但还是鼓励我不要管它。 :)
我仍然是Java的新手,所以我希望这种解释有意义。