针对返回值的问题?
class HighArray
{
private long[] a; //ref of array a
private int nElems; //number of data item
public HighArray(int max)
{
a=new long[max]; //create the array
nElems=0;
} //no item yet
public boolean find(long searchKey)
{
int j;
for(j=0;j <nElems;j++)
if(a[j]==searchKey)
break;
if(j==nElems)
return false;
else
return true;
}
public void insert(long value)
{
a[nElems]=value;
nElems++;
}
public boolean delete(long value)
{
int j;
for(j=0;j <nElems;j++)
if(value==a[j])
break;
if(j==nElems)
return false;
else
{
for(int k=j;k <nElems;k++)
a[k]=a[k+1];
nElems--;
return true;
}
}
public void display()
{
int j;
for(j=0;j <nElems;j++)
System.out.print(a[j]+ " ");
System.out.println( " ");
}
}
class HighArrayApp
{
public static void main(String[] args)
{
int maxsize=100;
HighArray arr;
arr=new HighArray(maxsize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display();
int searchKey=99;
if(arr.find(searchKey))
System.out.println( "已经找到 "+searchKey);
else
System.out.println( "没有找到 "+searchKey);
arr.delete(00);
arr.delete(55);
arr.delete(99);
arr.display();
}
}
在delete方法中删除完后return true/false结果一样,有可以避免的吗?
------解决方案--------------------你说delete方法中返回的结果吗?
我试了下,是正常的啊
------解决方案--------------------把arr.delete(99);中的参数该成一个没有的如91那么,arr.delete(91)的结果是false