package datestructure;
class Array
{
private long[] a;
private int
nElems;
public Array(int
maxsize)
{
a=new
long[maxsize];
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[j])
break;
if(j==nElems)
return
false;
else
{
for(int
k=j;k<nElems;k++)
a[k]=a[k+1];
nElems--;
return
true;
}
}
public long
getmax()
{
int j=0;
long
max=a[0];
if(nElems==0)
return
-1;
else
{
for(j=0;j<nElems-1;j++)
if(a[j]>max)
max=a[j];
return
max;
}
}
public void
removemax()
{
int j=0,k=0;
long
max=a[0];
for(j=0;j<nElems-1;j++)
if(a[j]>max)
{max=a[j];
k=j;}
for(;k<nElems;k++)
a[k]=a[k+1];
nElems--;
}
public
void display()
{
int
j;
for(j=0;j<nElems-1;j++)
System.out.print(a[j]+"
");
System.out.println("");
}
}
/////////////////////////////////////
package datestructure;
class ArrayApp
{
public static void main(String[]
args)
{
int
maxsize=100;
Array array;
array=new
Array(maxsize);
array.insert(22);
array.insert(33);
array.insert(55);
array.insert(88);
array.insert(44);
array.insert(66);
array.insert(11);
array.insert(77);
array.insert(99);
array.insert(00);
array.display();
int searchkey=55;
if(array.find(searchkey))
System.out.println("Found
"+ searchkey);
else
System.out.println("Can‘t Found "+searchkey);
array.delete(00);
array.delete(55);
array.delete(99);
array.display();
System.out.println(array.getmax());
array.removemax();
array.display();
}
}
原文:http://www.cnblogs.com/growAriel/p/3600710.html