SIDROTUL MUNAWAROH
Konsep dan Pemakaian Array
Java menyediakan struktur data, array, yang menyimpan kumpulan elemen berurutan berukuran tetap dari tipe yang sama. Sebuah array digunakan untuk menyimpan kumpulan data, tetapi seringkali lebih berguna untuk menganggap array sebagai kumpulan variabel dengan tipe yang sama.
1. Array App
- * Write a description of class ArrayApp here.
- *
- * @author
- * @version
- */
- public class ArrayApp
- {
- public static void main(String [] args){
- // declares an Array
- long[]arr;
- arr = new long[100];
- int nElems = 0;
- arr[0] = 77;
- arr[1] = 99;
- arr[2] = 44;
- arr[3] = 55;
- arr[4] = 22;
- arr[5] = 88;
- arr[6] = 11;
- arr[7] = 00;
- arr[8] = 66;
- arr[9] = 33;
- nElems = 10;
- int j;
- long searchKey;
- // display items of Array
- for(j=0; j<nElems; j++)
- System.out.print(arr[j] +" ");
- System.out.println(" ");
- //search items
- 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 items
- searchKey = 55; // delete item with key 55
- for(j=0; j<nElems; j++)
- if(arr[j] == searchKey)
- break;
- //deleting items
- for(int k=j; k<nElems; k++)
- arr[k] = arr[k+1];
- nElems--;
- //display items
- for(j=0; j<nElems; j++)
- System.out.print(arr[j]+" ");
- System.out.print(" ");
- }
- }
Output
2. Low Array
- /**
- * Write a description of class LowArray here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- 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){
- // declares an Array
- 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 items of Array
- for(j=0; j<nElems; j++)
- System.out.print(arr.getElem(j)+ " ");
- System.out.println(" ");
- //Search items
- 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 items
- for(j=0; j<nElems; j++) //search items
- if(arr.getElem(j) == 55)
- break;
- //deleting items
- for(int k=j; k<nElems; k++)
- arr.setElem(k,arr.getElem(k+1));
- nElems--;
- //Display items
- for(j=0; j<nElems; j++)
- System.out.print(arr.getElem(j)+" ");
- System.out.println(" ");
- }
- }
Output
3. Ordered Array
- * Write a description of class HighArray here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- 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);
- // display items
- arr.display();
- // search items
- int searchKey = 35;
- if(arr.find(searchKey))
- System.out.println("Found "+ searchKey);
- else
- System.out.println("Can't find "+ searchKey);
- // delete items
- arr.delete(0);
- arr.delete(55);
- arr.delete(99);
- // display items
- arr.display();
- }
- }
Output
4. Class Data Array
- /**
- * Write a description of class Person here.
- *
- * @author (your name)
- * @version (a version number or a date)
- */
- 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 items
- 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 items
- arr.displayA();
- //search items
- 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 items
- System.out.println("Deleting Smith, Yee, and Creswell");
- arr.delete("Smith");
- arr.delete("Yee");
- arr.delete("Creswell");
- //display items
- arr.displayA();
- }
- }
Output
Komentar
Posting Komentar