0
public int find(long searchKey)
{
int j;
for(j=0; j<nElems; j++) { // for each element,
if(a[j] == searchKey) // found search value?
return j; // then exit loop before end (exits entire function)
}
// reached end of for loop
return nElems; // thus, can't find it
} // end find()
public void insert(long value)
{
a[nElems] = value;
nElems++;
}
我想實現此功能返回搜索鍵的索引,而不是搜索鍵的值。這裏要說的是我不能讓顯示索引數組索引HighArray和HighArrayApp?
int searchKey = 33; // search for item
int indexToDisplay = j
if(arr.find(searchKey))
System.out.println("Found " + searchKey + " at index: " + j);
else
System.out.println("-1");
你爲什麼說'如果(arr.find(searchKey))'?對於任何非零值都是如此,除非我們正在查看兩種不同的方法。另外,你說你不能讓它顯示索引;它代表什麼?錯誤的索引? -1? – Dannnno 2015-02-06 18:32:18
現在的輸出是什麼? – phil652 2015-02-06 18:32:35