java问题: 不能执行删除方法
packagehomework;publicclassHighArray{privatelong[]a;privateintnElems;////publicHighAr...
package homework;
public class HighArray {
private long[] a;
private int nElems;
////
public HighArray(int max){
a = new long[max];
nElems = 0;
}
////
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[nElems])
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(){
for(int 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(88);
arr.insert(99);
arr.insert(44);
arr.insert(00);
arr.insert(11);
arr.insert(66);
arr.insert(22);
arr.display();
int searchKey = 35;
if(arr.find(searchKey))
System.out.println("Found" + searchKey);
else
System.out.println("Can't find" + searchKey);
arr.delete(77);
arr.delete(66);
arr.display();
}
} 展开
public class HighArray {
private long[] a;
private int nElems;
////
public HighArray(int max){
a = new long[max];
nElems = 0;
}
////
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[nElems])
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(){
for(int 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(88);
arr.insert(99);
arr.insert(44);
arr.insert(00);
arr.insert(11);
arr.insert(66);
arr.insert(22);
arr.display();
int searchKey = 35;
if(arr.find(searchKey))
System.out.println("Found" + searchKey);
else
System.out.println("Can't find" + searchKey);
arr.delete(77);
arr.delete(66);
arr.display();
}
} 展开
1个回答
展开全部
class HighArray {
private long[] a;
private int nElems;
////
public HighArray(int max){
a = new long[max];
nElems = 0;
}
////
////
public void insert(long value){
a[nElems] = value;
nElems++;
}
////修改后的删除方法
public boolean delete(long value){
int k=0;
for(int j=0;j<nElems;j++)
{
if(value==a[j])
{
k=j;
//调换顺序
for(;k+1<nElems;k++)
{
a[k] = a[k+1];
}
nElems--;
return true;
}
}
System.out.println("gggggggg");
return false;
}
///
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 display(){
for(int j=0;j<nElems;j++)
System.out.print(a[j] + " ");
System.out.println("");
}
}
//////
public class HighArrayApp{
public static void main(String[] args){
int maxsize = 100;
HighArray arr;
arr = new HighArray(maxsize);
arr.insert(77);
arr.insert(88);
arr.insert(99);
arr.insert(44);
arr.insert(00);
arr.insert(11);
arr.insert(66);
arr.insert(22);
arr.display();
System.out.println(arr.delete(00));
arr.display();
int searchKey = 35;
if(arr.find(searchKey))
System.out.println("Found" + searchKey);
else
System.out.println("Can't find" + searchKey);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询