由数组的封装来浅谈JAVA面向对象的思路
2009-05-29 19:49
567 查看
//demonstrates Java arrays
//基本操作:创建一个数组(插入),查找、删除数据项,显示。
class ArrayApp {
public static void main(String[] args) {
long[] arr; // reference to array
arr = new long[100]; // make array
int nElems = 0; // number of items
int j; // loop counter
long searchKey; // key of item to search for
arr[0] = 77; // insert 10 items
arr[1] = 99;
arr[2] = 44;
arr[3] = 55;
arr[4] = 22;
arr[5] = 88;
arr[6] = 11;
arr[7] = 00;
arr[8] = 66;
arr[9] = 33;
nElems = 10; // now 10 items in array
for (j = 0; j < nElems; j++)
System.out.print(arr[j] + " ");
System.out.println("");
searchKey = 66; // find item with key 66
for (j = 0; j < nElems; j++)
if (arr[j] == searchKey) // found item?
break; // yes, exit before end
if (j == nElems) // at the end?
System.out.println("Can't find " + searchKey); // yes
else
System.out.println("Found " + searchKey); // no
searchKey = 55; // delete item with key 55
for (j = 0; j < nElems; j++)
// look for it
if (arr[j] == searchKey)
break;
for (int k = j; k < nElems; k++)
// move higher ones down
arr[k] = arr[k + 1];
nElems--; // decrement size
for (j = 0; j < nElems; j++)
// display items
System.out.print(arr[j] + " ");
System.out.println("");
} // end main()
} // end class ArrayApp
将普通的Java数组封装在HighArray类中,从而使数组隐藏起来,属性是私有的,外部无法访问,只提供接口(方法)。类的用户HighArrayApp通过类接口与类相连。
//demonstrates array class with high-level interface
//基本操作:创建一个数组(插入),查找、删除数据项,显示。
class HighArray {
private long[] a; // ref to array a
private int nElems; // number of data items
public HighArray(int maxSize) {
a = new long[maxSize];
nElems = 0;
}
public void insert(long value) {
a[nElems] = value;
nElems ++;
}
public void display() {
for(int j=0; j<nElems; j++)
System.out.print(a[j] + " ");
System.out.println();
}
public void find(long searchKey) {
int j;
for(j=0; j<nElems; j++)
if(j == searchKey)
break;
if(j == nElems) {
System.out.println("Can't find " + searchKey);
}
else System.out.println("Can find " + searchKey);
}
public void delete(long value) {
int j;
for(j=0; j<nElems; j++)
if(a[j] == value)
break;
if(j == nElems) {
System.out.println(value + " isn't in array, Can't delet.");
}
else {
System.out.println(value + " is in array.");
for(int k=j; k<nElems; k++)
a[k] = a[k+1];
nElems --;
}
}
} // end class HighArray
class HighArrayApp {
public static void main(String[] args) {
int maxSize = 100; // array size
HighArray arr; // reference to array
arr = new HighArray(maxSize); // create the array
arr.insert(77); // insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display(); // display items
arr.find(35);
arr.delete(00); // delete 3 items
arr.delete(55);
arr.delete(99);
arr.display(); // display items again
} // end main()
} // end class HighArrayApp
//基本操作:创建一个数组(插入),查找、删除数据项,显示。
class ArrayApp {
public static void main(String[] args) {
long[] arr; // reference to array
arr = new long[100]; // make array
int nElems = 0; // number of items
int j; // loop counter
long searchKey; // key of item to search for
arr[0] = 77; // insert 10 items
arr[1] = 99;
arr[2] = 44;
arr[3] = 55;
arr[4] = 22;
arr[5] = 88;
arr[6] = 11;
arr[7] = 00;
arr[8] = 66;
arr[9] = 33;
nElems = 10; // now 10 items in array
for (j = 0; j < nElems; j++)
System.out.print(arr[j] + " ");
System.out.println("");
searchKey = 66; // find item with key 66
for (j = 0; j < nElems; j++)
if (arr[j] == searchKey) // found item?
break; // yes, exit before end
if (j == nElems) // at the end?
System.out.println("Can't find " + searchKey); // yes
else
System.out.println("Found " + searchKey); // no
searchKey = 55; // delete item with key 55
for (j = 0; j < nElems; j++)
// look for it
if (arr[j] == searchKey)
break;
for (int k = j; k < nElems; k++)
// move higher ones down
arr[k] = arr[k + 1];
nElems--; // decrement size
for (j = 0; j < nElems; j++)
// display items
System.out.print(arr[j] + " ");
System.out.println("");
} // end main()
} // end class ArrayApp
将普通的Java数组封装在HighArray类中,从而使数组隐藏起来,属性是私有的,外部无法访问,只提供接口(方法)。类的用户HighArrayApp通过类接口与类相连。
//demonstrates array class with high-level interface
//基本操作:创建一个数组(插入),查找、删除数据项,显示。
class HighArray {
private long[] a; // ref to array a
private int nElems; // number of data items
public HighArray(int maxSize) {
a = new long[maxSize];
nElems = 0;
}
public void insert(long value) {
a[nElems] = value;
nElems ++;
}
public void display() {
for(int j=0; j<nElems; j++)
System.out.print(a[j] + " ");
System.out.println();
}
public void find(long searchKey) {
int j;
for(j=0; j<nElems; j++)
if(j == searchKey)
break;
if(j == nElems) {
System.out.println("Can't find " + searchKey);
}
else System.out.println("Can find " + searchKey);
}
public void delete(long value) {
int j;
for(j=0; j<nElems; j++)
if(a[j] == value)
break;
if(j == nElems) {
System.out.println(value + " isn't in array, Can't delet.");
}
else {
System.out.println(value + " is in array.");
for(int k=j; k<nElems; k++)
a[k] = a[k+1];
nElems --;
}
}
} // end class HighArray
class HighArrayApp {
public static void main(String[] args) {
int maxSize = 100; // array size
HighArray arr; // reference to array
arr = new HighArray(maxSize); // create the array
arr.insert(77); // insert 10 items
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(00);
arr.insert(66);
arr.insert(33);
arr.display(); // display items
arr.find(35);
arr.delete(00); // delete 3 items
arr.delete(55);
arr.delete(99);
arr.display(); // display items again
} // end main()
} // end class HighArrayApp
相关文章推荐
- 【黑马程序员】方法、数组、面向对象、封装、继承——Java复习笔记
- 浅谈Java面向对象三大特征--封装,多态
- 黑马程序员_java封装-继承-多态为面向对象的三大基石
- 复习java面向对象(封装)
- java面向对象的三大基本特征之封装,继承,多态
- Java面向对象的三个基本特征:封装、继承、多态
- 浅谈java中集合的由来,以及集合和数组的区别详解
- Java程序员从笨鸟到菜鸟之(二)面向对象之封装,继承,多态(上)
- Java 入门 之 面向对象 封装、继承与多态(二)
- JAVA 面向对象 隐藏和封装
- JAVA初等基础__(面向对象三个特征, 封装,继承,多态)
- java数组浅谈
- 【职坐标】java面向对象三大特征(一)——封装
- java总结第二次//数组及面向对象
- 笔记整理(java基础三-------面向对象--封装)
- 浅谈一下大数相除有关思路(图解)与用java代码具体解决方案
- Java 面向对象之封装
- Java面试题多思路解析--有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中
- java面向对象之封装
- 智渔课堂官方免费教程十三:Java面向对象三大特性之封装