PENUGASAN 2 -- Konsep dan Pemakaian Array
ARRAY is a data structure that contains homogeneous elements. In java, arrays are dinamically allocated. All elements are stored under one name. Occupies contiguous memory location.
Types of Arrays:
1. One-demensional array
it is also knowm as linear array. the elements are stored in a single row.
2. Two-dimensional array
Two-dimensional array stores the data in rows and columns.
3. Multi-dimensional array
Combination of two more arrays or nested arrays.
Example of Array
1. Array App
source:
/** * Array App * * @author Adelia Hasna Surya Putri * */ 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(" "); } }
output:
2. Low Array
source:
/** * Low Array * * @author Adelia Hasna Surya Putri * */ 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(" "); } }
output:
3. High Array
source:
/** * High Array * * @author Adelia Hasna Surya Putri * */ 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(); } }
output:
4. Ordered Array
source:
/** * Ordered Array * * @author Adelia Hasna Surya Putri * */ 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(); } }
output:
5. Class Data Array
source:
/** * Class Data Array * * @author Adelia Hasna Surya Putri * */ 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(); } }
output:
Adelia Hasna Surya Putri/5025201200
29/03/2021






Comments
Post a Comment