您的位置:首页 > 编程语言 > Java开发

Java数组基础综合案例

2015-04-22 17:43 447 查看
class HighArray
{
private long[] a;
private int nElems;
private int j;

public HighArray(int Max)
{
a = new long[Max];
nElems = 0;
}

public boolean find(long searchKey)
{
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 searchKey)
{
for(j=0;j<nElems;j++)
if(a[j]==searchKey)
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(j=0;j<nElems;j++)
System.out.print(a[j]+" ");
System.out.println();
}

public long getMax()
{
long max=a[0];
for(j=0;j<nElems;j++)
{
if(a[j]>max)
max=a[j];
else
continue;
}
return max;
}

public long getMin()
{
long min=a[0];
for(j=0;j<nElems;j++)
{
if(a[j]<min)
min=a[j];
else
continue;
}
return min;
}

public long ave()
{
long sum=0;
for(j=0;j<nElems;j++)
sum+=a[j];
return sum/nElems;
}
}

public 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(0);
arr.insert(66);
arr.insert(33);

arr.display();

int searchKey=35;
if(arr.find(searchKey))
System.out.println("Find "+searchKey);
else
System.out.println("Can't Find "+searchKey);

arr.delete(0);
arr.delete(55);
arr.delete(99);

arr.display();

System.out.println("最大值是:"+arr.getMax());
System.out.println("最小值是:"+arr.getMin());
System.out.println("平均值是:"+arr.ave());

arr.delete(arr.getMax());
arr.display();
}
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: