我正在尝试使用数组实现线性搜索。应该搜索一个人的名字,如果名字存在,则返回相应的数字,如果不存在,则返回-1。
我为2种情况编写了junit测试-名称存在(testLinearSearchOK)而名称不存在(testLinearSearchFail)。
到目前为止,我仅使用以下代码设法通过了testLinearSearchOK:
public class ASearch {
private Entry[] catalogue;
private int current;
/*
* Assume 10 entries
*/
public ASearch(){
catalogue = new Entry[10];
current = 0;
}
/*
* Ignores adding if full (should really be handled by exception...)
*/
public void addEntry(Entry e){
if(current < 10){
catalogue[current++] = e;
}
}
public int linearSearch(String name){
int current = 0;
while (!catalogue[current].getName().equals(name))
current++;
return catalogue[current].getNumber();
}
}
getName和getNumber方法分别返回名称和数字。
这是我编写的测试:
@Before
public void setup(){
as = new ASearch();
as.addEntry(new Entry("Pete",111));
as.addEntry(new Entry("Ken",123));
as.addEntry(new Entry("Tim",222));
}
@Test
public void testLinearSearchOK() {
assertEquals(123, as.linearSearch("Ken"));
}
@Test
public void testLinearSearchFail() {
assertEquals(-1, as.linearSearch("Leo"));
}
关于如何使testLinearSearchFail正常运行的任何提示?
Eclipse的堆栈跟踪(尼古拉斯的回答):
java.lang.NullPointerException
at ASearch.linearSearch(ASearch.java:29)
at ASearchTest.testLinearSearchFail(ASearchTest.java:30)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:50)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:47)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:89)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:41)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:541)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)
答案 0 :(得分:1)
跟踪数字而不是索引:
int number = -1;
for(int i = 0; i < catalogue.length; i++) {
if(catalogue[i].getName().equals(name)) {
number = catalogue[i].getNumber();
break;
}
}
return number;
答案 1 :(得分:1)
避免多次返回,当您尝试分析该方法的作用时就是噩梦。
int linearSearch(String name) {
int result = -1;
for (CatalogueEntry entry : catalogue) {
if (name.equals(catalogue.name()) {
result = catalogue.getNumber();
break;
}
}
return result;
}
答案 2 :(得分:0)
将代码更改为:
public static int linearSearch(String name) {
for (int i = 0; catalogue[i] != null; i++) {
if (name.equals(catalogue[i].getName())) {
return catalogue[i].getNumber(); // <--- return early if match found
}
}
return -1; // <--- return -1 for no match
}
逻辑:
代码失败的原因是,即使找不到匹配项,您也尝试从数组catalogue
返回数字。