数据结构与算法3

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

要抓紧喽~~~~~~~放羊的孩纸回来喽

 LowArray类和LowArrayApp类

程序将一个普通的Java数组封装在LowArray类中。类中的数组隐藏了起来,它是私有的,所以只有类自己的方法才能访问他。

LowArrayApp类创建了一个LowArray类的对象并用它储存和操作数据。可以将LowArray类想成一个工具,LowArrayApp类是工具的使用者。现在程序被划分为两个各自扮演不同角色的类。

用来储存数据对象的类有时被称为容器类(container class),例如在LowArray.java中的LowArray类。通常容器类不仅存储数据,并且提供访问数据的方法和其他诸如排序等复杂的操作。


class LowArray
{
private long[] a;
public LowArray(int size)
{
	a = new long[size];		
}
public void setElem(int index,long value)
{
	a[index] = value;
}
public long getElem(int index)
{
	return a[index];
}
}
class lowArrayApp
{
public static void main(String[] args)
{
LowArray arr;
arr = new LowArray(100);
int nElems = 0;
int j;
arr.setElem(0,77);
arr.setElem(1,99);
arr.setElem(2,44);
arr.setElem(3,55);
arr.setElem(4,22);
arr.setElem(5,88);
arr.setElem(6,11);
arr.setElem(7,00);
arr.setElem(8,66);
arr.setElem(9,33);
nElems = 10;
for(j = 0;j
77 99 44 55 22 88 11 0 66 33  
no found26
77 99 44 22 88 11 0 66 33  

 

转载于:https://my.oschina.net/u/3829307/blog/1859104

你可能感兴趣的:(数据结构与算法3)