Tugas 2, Konsep dan Pemakaian Array

 

Konsep dan Pemakaian Array

An array is a data structure that stores one or more similar type of values in a single value. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

There are three different kind of arrays and each array value is accessed using an ID c which is called array index.

  • Numeric array − An array with a numeric index. Values are stored and accessed in linear fashion.

  • Associative array − An array with strings as index. This stores element values in association with key values rather than in a strict linear index order.

  • Multidimensional array − An array containing one or more arrays and values are accessed using multiple indices


1. ArrayAPP
 
Source code: 
/**
* Array App
*
* @author Nabila Zakiyah Khansa
*
*/
public class ArrayApp
{
public static void main(String[] args){
//ARRAY
long[] arr;
int nElems = 0;
arr = new long[100];
arr[0] = 77;
arr[1] = 99;
arr[2] = 44;
arr[3] = 55;
arr[4] = 22;
arr[5] = 88;
arr[6] = 11;
arr[7] = 0;
arr[8] = 66;
arr[9] = 33;
nElems = 10;
int j;
long searchKey;
//Display Elements of Array
for(j = 0; j < nElems; j++)
System.out.print(arr[j] + " ");
System.out.println(" ");
//Search Element
searchKey = 66; //Find item with key 66
for(j = 0; j < nElems; j++)
if(arr[j] == searchKey)
break;
if(j == nElems)
System.out.println("Can't find "+ searchKey);
else
System.out.println("Found "+ searchKey);
//Delete Element
searchKey = 55; //Delete item with 55
for(j = 0; j < nElems; j++)
if(arr[j] == searchKey)
break;
//deleting
for(int k = j; k < nElems; k++)
arr[k] = arr[k+1];
nElems--;
//Display Elements
for(j = 0; j < nElems; j++)
System.out.print(arr[j] + " ");
System.out.println(" ");}
}
view raw ArrayApp hosted with ❤ by GitHub
output:





2. LowArray

Source code:
/**
* Low Array
*
* @author Nabila Zakiyah Khansa
*
*/
class LowArray
{
private long[] a;
public LowArray(int size){
a = new long[size];
}
public void setElem(int index, long value){
a[index] = value;
}
public long getElem(int index){
return a[index];
}
}
public class LowArrayApp
{
public static void main(String[] args){
LowArray arr;
arr = new LowArray(100);
int nElems=0;
int j;
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,66);
arr.setElem(8,0);
arr.setElem(9,33);
nElems = 10;
//Display Elements of Array
for(j = 0; j < nElems; j++)
System.out.print(arr.getElem(j)+ " ");
System.out.println(" ");
//Search Element
int searchKey = 26;
for(j = 0; j < nElems; j++)
if(arr.getElem(j) == searchKey)
break;
if(j == nElems)
System.out.println("Can't find "+ searchKey);
else
System.out.println("Found "+ searchKey);
//Delete Element
//search element
for(j = 0; j < nElems; j++)
if(arr.getElem(j) == 55)
break;
//deleting element
for(int k = j; k < nElems; k++)
arr.setElem(k, arr.getElem(k+1));
nElems--;
//Display Elements
for(j = 0; j < nElems; j++)
System.out.print(arr.getElem(j)+" ");
System.out.println(" ");
}
}
view raw LowArray hosted with ❤ by GitHub
output:

3. HighArray

Source code: 
/**
* High Array
*
* @author Nabila Zakiyah Khansa
*
*/
class HighArray
{
private long[] a;
private int nElems;
public HighArray(int max){
a = new long[max];
nElems = 0;
}
public boolean find(long searchKey){
int j;
for(j = 0; j <nElems; j++)
if(a[j] == searchKey)
break;
if(j == nElems)
return false;
else
return true;
}
public void insert(long value){
a[nElems] = value;
nElems++;
}
public boolean delete(long value){
int j;
for(j = 0; j < nElems; j++)
if(value == a[j])
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++)
System.out.print(a[j]+" ");
System.out.println(" ");
}
}
public class HighArrayApp{
public static void main(String[] args){
int maxSize = 100;
HighArray arr;
arr = new HighArray(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(0);
arr.insert(66);
arr.insert(33);
arr.display();
//Search Element
int searchKey = 35;
if(arr.find(searchKey))
System.out.println("Found "+searchKey);
else
System.out.println("Can't find "+searchKey);
//Delete Elements
arr.delete(0);
arr.delete(55);
arr.delete(99);
//Display Elements
arr.display();
}
}
view raw HighArray hosted with ❤ by GitHub
output:


4. OrderredArray

Source code: 
/**
* Ordered Array
*
* @author Nabila Zakiyah Khansa
*
*/
class OrderedArray
{
private long[] a;
private int nElems;
public OrderedArray(int max){
a = new long[max];
nElems = 0;
}
public int size(){
return nElems;
}
public int find(long searchKey){
int lowerBound = 0;
int upperBound = nElems - 1;
int curln;
while(true){
curln = (lowerBound + upperBound) / 2;
if(a[curln] == searchKey)
return curln;
else if(lowerBound > upperBound)
return nElems;
else{
if(a[curln] < searchKey)
lowerBound = curln + 1;
else
upperBound = curln - 1;
}
}
}
public void insert (long value){
int j;
for(j = 0; j < nElems; j++)
if(a[j] > value)
break;
for(int k = nElems; k > j; k--)
a[k] = a[k-1];
a[j] = value;
nElems++;
}
public boolean delete(long value){
int j = find(value);
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++)
System.out.print(a[j]+" ");
System.out.println(" ");
}
}
public class OrderedArrayApp{
public static void main(String[] args){
int maxSize = 100;
OrderedArray arr;
arr = new OrderedArray(maxSize);
arr.insert(77);
arr.insert(99);
arr.insert(44);
arr.insert(55);
arr.insert(22);
arr.insert(88);
arr.insert(11);
arr.insert(0);
arr.insert(66);
arr.insert(33);
//Search Element
int searchKey = 55;
if(arr.find(searchKey) != arr.size())
System.out.println("Found "+searchKey);
else
System.out.println("Can't find "+searchKey);
//Display Elements
arr.display();
//Delete Elements
arr.delete(0);
arr.delete(55);
arr.delete(99);
//Display Elements After Removing Some Elements
arr.display();
}
}
view raw OrderredArray hosted with ❤ by GitHub
output:


5. Classdata Array

Source code: 
/**
* Class Data Array
*
* @author Nabila Zakiyah Khansa
*
*/
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("First 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 displayA(){
for(int j = 0; j < nElems; j++)
a[j].displayPerson();
}
}
public class ClassDataApp{
public static void main(String[] args){
int maxSize = 100;
ClassDataArray arr;
arr = new ClassDataArray(maxSize);
//Insert Elements
arr.insert("Evans", "Patty", 24);
arr.insert("Smith", "Loraine", 37);
arr.insert("Yee", "Tom", 43);
arr.insert("Adams", "Henry", 63);
arr.insert("Hashimoto", "Sato", 21);
arr.insert("Stimson", "Henry", 29);
arr.insert("Velasquez", "Jose", 72);
arr.insert("Lamarque", "Henry", 54);
arr.insert("Vang", "Minh", 22);
arr.insert("Creswell", "Lucinda", 18);
//Display Elements
arr.displayA();
//Search Element
String searchKey = "Stimson";
Person found;
found = arr.find(searchKey);
if(found !=null){
System.out.println("Found ");
found.displayPerson();
}
else
System.out.println("Can't find "+searchKey);
//Delete Elements
System.out.println("Deleting Smith, Yee, and Creswell");
arr.delete("Smith");
arr.delete("Yee");
arr.delete("Creswell");
//Display Elements
arr.displayA();
}
}
view raw ClassdataArray hosted with ❤ by GitHub
output:


Komentar

Postingan populer dari blog ini

Final Project - Eas

Tugas 1 - PPB F