数组索引 HighArray 和 HighArrayApp?
Array Indexing HighArray and HighArrayApp?
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++;
}
我正在尝试将此功能实现到 return 搜索键的索引,而不是搜索键的值。这是主要的 class,我无法显示索引
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");
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");
您实际上将 indexToDisplay 设置为空,您要做的是将其设置为 find() 方法的 return 值,就像这样:
int indexToDisplay = arr.find(searchKey);
然后,由于您的查找方法不是 return 布尔值,您可以检查它是否是数组中的有效索引,如下所示:
if(indexToDisplay != -1)
System.out.println("Found " + searchKey + " at index: " + j );
我不太确定 nElems 是什么,但是如果您在数组中找不到您要查找的内容,我会推荐 returning -1。所以我们总共有这样的东西:
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 -1; // thus, can't find it
} // end find()
public void insert(long value)
{
a[nElems] = value;
nElems++;
}
我们这样称呼它:
int searchKey = 33; // search for item
int indexToDisplay = arr.find(searchKey);
if(indexToDisplay != -1)
System.out.println("Found " + searchKey + " at index: " + indexToDisplay );
else
System.out.println("-1");
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++;
}
我正在尝试将此功能实现到 return 搜索键的索引,而不是搜索键的值。这是主要的 class,我无法显示索引
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");
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");
您实际上将 indexToDisplay 设置为空,您要做的是将其设置为 find() 方法的 return 值,就像这样:
int indexToDisplay = arr.find(searchKey);
然后,由于您的查找方法不是 return 布尔值,您可以检查它是否是数组中的有效索引,如下所示:
if(indexToDisplay != -1)
System.out.println("Found " + searchKey + " at index: " + j );
我不太确定 nElems 是什么,但是如果您在数组中找不到您要查找的内容,我会推荐 returning -1。所以我们总共有这样的东西:
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 -1; // thus, can't find it
} // end find()
public void insert(long value)
{
a[nElems] = value;
nElems++;
}
我们这样称呼它:
int searchKey = 33; // search for item
int indexToDisplay = arr.find(searchKey);
if(indexToDisplay != -1)
System.out.println("Found " + searchKey + " at index: " + indexToDisplay );
else
System.out.println("-1");