Data structure and algorithm 6

This program shows that the data storage structure can handle class objects in the same way as simple types.

Note that the keyword is a String class object, so you need to use the equals() method instead of the = = operator when making a comparison.

 

class Person{
	private String lastName;
	private String firstName;
	private int age;
	public Person(String last,String first,int a)
	{
		lastName = last;
		firstName = first;
		age = a;
	}
	public void displayPerson()
	{
		System.out.println(" Last name: " + lastName);
		System.out.println(" fisrt name: " + firstName);
		System.out.println(" age " + age);
	}
	public String getLast()
	{
		return lastName;
	}
}
class classDataArray {
	private Person[] a;
	private int nElems;
	public classDataArray(int max)
	{
		a = new Person[max];
		nElems = 0;
	}
	public Person find(String searchName) {
		int j;
		for(j=0;j<nElems;j++)
			if(a[j].getLast().equals(searchName))
				break;
		if(j == nElems)
			return null;
		else
		return a[j];
		
	}
	public void insert(String last,String first,int age)
	{
		a[nElems] = new Person(last,first,age);
		nElems++;
	}
	public boolean delete(String searchName)
	{
		int j;
		for(j=0;j<nElems;j++)
			if(a[j].getLast().equals(searchName))
			break;
		if(j==nElems)
			return false;
		else
		{
			for(int k=j;k<nElems;k++)
				a[k] = a[k+1];
			nElems--;
			return true;
		}
	}
	public void display()
	{
		for(int j=0;j<nElems;j++)
			a[j].displayPerson();
	}
	
}
class ClassDataApp{
	public static void main(String[] args)
	{
		int maxSize = 100;
		classDataArray arr;
		arr = new classDataArray(maxSize);
		arr.insert("e", "patty", 24);
		arr.insert("s", "lorr", 37);
		arr.insert("yee", "tom", 46);
		arr.insert("adams", "henry", 63);
		arr.insert("hashinte", "sate", 21);
		arr.insert("stimson", "henry", 29);
		arr.insert("velasquez", "jose",72);
		arr.insert("lamrque", "henry", 54);
		arr.insert("vang", "winth",22);
		arr.insert("creswekk", "lus", 18);
		arr.display();
		String searchkey = "e";
		Person found;
		found = arr.find(searchkey);
		if(found != null)
		{
			System.out.println("found");;
			found.displayPerson();
		}
		else
			System.out.println("no found" + searchkey);
		System.out.println("delete s,yee,adams");
		arr.delete("s");
		arr.delete("yee");
		arr.delete("adams");
		arr.display();
		
		
	}
}
 Last name: e
 fisrt name: patty
 age 24
 Last name: s
 fisrt name: lorr
 age 37
 Last name: yee
 fisrt name: tom
 age 46
 Last name: adams
 fisrt name: henry
 age 63
 Last name: hashinte
 fisrt name: sate
 age 21
 Last name: stimson
 fisrt name: henry
 age 29
 Last name: velasquez
 fisrt name: jose
 age 72
 Last name: lamrque
 fisrt name: henry
 age 54
 Last name: vang
 fisrt name: winth
 age 22
 Last name: creswekk
 fisrt name: lus
 age 18
found
 Last name: e
 fisrt name: patty
 age 24
delete s,yee,adams
 Last name: e
 fisrt name: patty
 age 24
 Last name: hashinte
 fisrt name: sate
 age 21
 Last name: stimson
 fisrt name: henry
 age 29
 Last name: velasquez
 fisrt name: jose
 age 72
 Last name: lamrque
 fisrt name: henry
 age 54
 Last name: vang
 fisrt name: winth
 age 22
 Last name: creswekk
 fisrt name: lus
 age 18

Arrays in java are objects created by the new operator

Unordered arrays can be inserted quickly, but find and delete are slow

Encapsulating arrays into classes protects them from arbitrary changes

Class interface and user accessible method composition

The design of class interface makes the operation of class user easier

Ordered arrays can be binary

The time required for linear search is proportional to the data

The time needed for binary search is directly proportional to the logarithm of the data

The big O representation provides a convenient way to compare the speed of the algorithm

O(1)>O(logN)>O(N)>O(N^2)

Algorithm from good to bad

Tags: Java

Posted on Fri, 31 Jan 2020 06:59:59 -0800 by zohab