Search, insert, delete, traverse of Java Analog Array (array)

Source: Internet
Author: User

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 ();
}
}


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.