1.1封装数组
程序员文章站
2022-03-22 19:56:04
...
package array;
public class LowArray {
private long[] a;
/**
* 构造函数
*
* @param size
*/
public LowArray(int size) {
a = new long[size];
}
/**
* 更新某个坐标的值
*
* @param point
* @param value
*/
public void setElem(int point, long value) {
a[point] = value;
}
/**
* 获得指定坐标值
* @param index
* @return long
*/
public long getElem(int index){
return a[index];
}
}
TEST
/**
* @param args
*/
public static void main(String[] args) {
LowArray arr=new LowArray(100);
int nElement = 0;
int j; // loop count
long searchKey;// Key Item
// insert 10 iteams
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);
nElement = 10;
// now 10 iteams in array
for (j = 0; j < nElement; j++) {
System.out.print(arr.getElem(j) + "\t");
}
System.out.println();
/*
* Search
*/
searchKey = 26;
for (j = 0; j < nElement; j++) {
if (arr.getElem(j) == searchKey) {
break;
}
}
if (j == nElement) {
System.out.println("Can't find " + searchKey);
} else {
System.out.println("Found " + searchKey);
}
/*
* delete Key
*/
searchKey = 55;
for (j = 0; j < nElement; j++) {
if (arr.getElem(j) == searchKey) {
break;
}
}
for (int k = 0; k < nElement; k++) {
arr.setElem(k, arr.getElem(k+1));
}
nElement--;
/*
* display
*/
for (j = 0; j < nElement; j++) {
System.out.print(arr.getElem(j) + "\t");
}
System.out.println();
}