gpt4 book ai didi

java - 使用数组通过人名线性搜索数字 - 如果名称不存在,方法将不起作用

转载 作者:行者123 更新时间:2023-11-30 01:55:30 25 4
gpt4 key购买 nike

我正在尝试使用数组实现线性搜索。它应该搜索一个人的名字,如果该名字存在则返回相应的号码,如果不存在则返回 -1。

我为两种情况编写了 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)

最佳答案

跟踪数字而不是索引:

int number = -1;

for(int i = 0; i < catalogue.length; i++) {
if(catalogue[i].getName().equals(name)) {
number = catalogue[i].getNumber();
break;
}
}

return number;

关于java - 使用数组通过人名线性搜索数字 - 如果名称不存在,方法将不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54675313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com