# Data structure and algorithm 3

We need to hold fast to it

LowArray class and LowArrayApp class

The program encapsulates a normal Java array in the LowArray class. The array in the class is hidden. It is private, so only the class's own methods can access it.

The LowArrayApp class creates an object of the LowArray class and uses it to store and manipulate data. You can think of the LowArray class as a tool, and the LowArrayApp class is the user of the tool. Now the program is divided into two classes that each play a different role.

The class used to store data objects is sometimes called the container class, such as the LowArray class in LowArray.java. In general, container classes not only store data, but also provide methods to access data and other complex operations such as sorting.

``````
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<nElems;j++)
System.out.print(arr.getElem(j)+" ");
System.out.println(" ");
int searchkey = 26;
for(j=0;j<nElems;j++)
if(arr.getElem(j) == searchkey)
break;
if(j==nElems)
System.out.println("no found" + searchkey);
else
System.out.println("found " + searchkey);
for(j=0;j<nElems;j++)
if(arr.getElem(j) == 55)
break;
for(int k = j;k<nElems;k++)
arr.setElem(k, arr.getElem(k+1));
nElems--;
for(j=0;j<nElems;j++)
System.out.print(arr.getElem(j)+" ");
System.out.println(" ");

}
}
``````
``````77 99 44 55 22 88 11 0 66 33
no found26
77 99 44 22 88 11 0 66 33  ``````

Tags: Java

Posted on Sun, 09 Feb 2020 14:16:21 -0500 by Cerberus_26