Package binary;
public class Person {
Private String LastName;
Private String FirstName;
private int age;
Public person (String ln,string fn,int age) {
THIS.LASTNAME=LN;
THIS.FIRSTNAME=FN;
This.age=age;
}
public void Displayperson () {
System.out.println ("surname:" +this.lastname);
System.out.println ("Name:" +this.firstname);
System.out.println ("Age:" +this.age);
}
Public String GetLast () {
return lastName;
}
}
Class classdataarray{
Reference to Array
Private person[] Perarray;
Number of data item
private int nitems;
/**
* Construct perarray[]
* @param maxLength
*/
Public Classdataarray (int maxLength) {
This.perarray=new Person[maxlength];
this.nitems=0;
}
/**
* Find Searchkey from array
* @param searchname
* @return Person
*/
Public person Find (String searchname) {
Find specified value
Int J;
For each element
for (j=0;j<nitems;j++) {
Found item
if (Perarray[j].getlast (). Equals (Searchname)) {
Exit Loop before end
Break
}
}
Gone to end
if (J==nitems) {
Yes,can ' t find it
return null;
}else{
No,found it
return PERARRAY[J];
}
}
/**
* Delete a element from array
* @param searchname
* @return
*/
public boolean Delete (String searchname) {
Delete person from Perarray
int j=0;
for (j=0;j<nitems;j++) {
if (Perarray[j].getlast (). Equals (Searchname)) {
Break
}
}
Can ' t find it
if (J==nitems) {
return false;
}else{
Shift Down
for (int k = j; k < Nitems; k++) {
if ((k+1) ==nitems) {
Break
}
PERARRAY[K]=PERARRAY[K+1];
}
Decrement size
nitems--;
return true;
}
}
public void Insert (String ln,string fn,int age) {
Perarray[nitems]=new person (ln,fn,age);
nitems++;
}
/**
* Display array Contents
*
*/
public void Displaya () {
For each elements
for (int i = 0; i < Nitems; i++) {
Perarray[i].displayperson ();
}
}
}
Package binary;
public class Classdataapp {
public static void Main (string[] args) {
Array size
int maxsize=10;
Reference to Array
Classdataarray arr=new Classdataarray (maxSize);
int ten Items
Arr.insert ("Tfq1", "TN1", 10);
Arr.insert ("Tfq2", "tn2", 10);
Arr.insert ("Tfq3", "Tn3", 10);
Arr.insert ("Tfq4", "Tn4", 10);
Arr.insert ("Tfq5", "Tn5", 10);
Arr.insert ("Tfq6", "Tn6", 10);
Arr.insert ("Tfq7", "tn7", 10);
Arr.insert ("Tfq8", "Tn8", 10);
Arr.insert ("Tfq9", "Tn9", 10);
Arr.insert ("Tfq10", "Tn10", 10);
Display array elements
Arr.displaya ();
Searchname
String searchname= "Tfq5";
Person P=arr.find (Searchname);
if (p!=null) {
P.displayperson ();
}else{
System.out.println ("No find it!");
}
System.out.println ("--------------------");
Delete three elements
Arr.delete ("Tfq6");
Arr.delete ("Tfq7");
Arr.delete ("Tfq8");
Arr.displaya ();
}
}