Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
115 changes: 59 additions & 56 deletions src/Chap02/LowArray/lowArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,66 +2,69 @@
// demonstrates array class with low-level interface
// to run this program: C>java LowArrayApp
////////////////////////////////////////////////////////////////
class LowArray
{
private long[] a; // ref to array a
//--------------------------------------------------------------
public LowArray(int size) // constructor
{ a = new long[size]; } // create array
//--------------------------------------------------------------
public void setElem(int index, long value) // set value
{ a[index] = value; }
//--------------------------------------------------------------
public long getElem(int index) // get value
{ return a[index]; }
//--------------------------------------------------------------
} // end class LowArray
class LowArray {
private long[] a; // ref to array a
//--------------------------------------------------------------
public LowArray(int size) // constructor
{
a = new long[size];
} // create array
//--------------------------------------------------------------
public void setElem(int index, long value) // set value
{
a[index] = value;
}
//--------------------------------------------------------------
public long getElem(int index) // get value
{
return a[index];
}
//--------------------------------------------------------------
} // end class LowArray
////////////////////////////////////////////////////////////////
class LowArrayApp
{
public static void main(String[] args)
{
LowArray arr; // reference
arr = new LowArray(100); // create LowArray object
int nElems = 0; // number of items in array
int j; // loop variable
class LowArrayApp {
public static void main(String[] args) {
LowArray arr; // reference
arr = new LowArray(100); // create LowArray object
int nElems = 0; // number of items in array
int j; // loop variable

arr.setElem(0, 77); // insert 10 items
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; // now 10 items in array
arr.setElem(0, 77); // insert 10 items
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; // now 10 items in array

for(j=0; j<nElems; j++) // display items
System.out.print(arr.getElem(j) + " ");
System.out.println("");
for (j = 0; j < nElems; j++) // display items
System.out.print(arr.getElem(j) + " ");
System.out.println("");

int searchKey = 26; // search for data item
for(j=0; j<nElems; j++) // for each element,
if(arr.getElem(j) == searchKey) // found item?
break;
if(j == nElems) // no
System.out.println("Can't find " + searchKey);
else // yes
System.out.println("Found " + searchKey);
int searchKey = 26; // search for data item
for (j = 0; j < nElems; j++) // for each element,
if (arr.getElem(j) == searchKey) // found item?
break;
if (j == nElems) // no
System.out.println("Can't find " + searchKey);
else // yes
System.out.println("Found " + searchKey);

// delete value 55
for(j=0; j<nElems; j++) // look for it
if(arr.getElem(j) == 55)
break;
for(int k=j; k<nElems; k++) // higher ones down
arr.setElem(k, arr.getElem(k+1) );
nElems--; // decrement size
// delete value 55
for (j = 0; j < nElems; j++) // look for it
if (arr.getElem(j) == 55)
break;
for (int k = j; k < nElems; k++) // higher ones down
arr.setElem(k, arr.getElem(k + 1));
nElems--; // decrement size

for(j=0; j<nElems; j++) // display items
System.out.print( arr.getElem(j) + " ");
System.out.println("");
} // end main()
} // end class LowArrayApp
for (j = 0; j < nElems; j++) // display items
System.out.print(arr.getElem(j) + " ");
System.out.println("");
} // end main()
} // end class LowArrayApp
////////////////////////////////////////////////////////////////