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

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL

  1. * Write a description of class ArrayApp here.
  2. *
  3. * @author
  4. * @version
  5. */
  6. public class ArrayApp
  7. {
  8.   public static void main(String [] args){
  9.     // declares an Array
  10.     long[]arr;
  11.     arr = new long[100];
  12.     int nElems = 0;
  13.  
  14.     arr[0] = 77;
  15.     arr[1] = 99;
  16.     arr[2] = 44;
  17.     arr[3] = 55;
  18.     arr[4] = 22;
  19.     arr[5] = 88;
  20.     arr[6] = 11;
  21.     arr[7] = 00;
  22.     arr[8] = 66;
  23.     arr[9] = 33;
  24.     nElems = 10;
  25.  
  26.     int j;
  27.     long searchKey;
  28.     // display items of Array
  29.     for(j=0; j<nElems; j++)
  30.       System.out.print(arr[j] +" ");
  31.       System.out.println(" ");
  32.  
  33.     //search items
  34.     searchKey = 66; // find item with key 66
  35.     for(j=0; j<nElems; j++)
  36.        if(arr[j] == searchKey)
  37.        break;
  38.  
  39.        if(j == nElems)
  40.           System.out.println("can't find" + searchKey);
  41.        else
  42.           System.out.println("Found" + searchKey);
  43.  
  44.      //delete items
  45.      searchKey = 55; // delete item with key 55
  46.      for(j=0; j<nElems; j++)
  47.        if(arr[j] == searchKey)
  48.          break;
  49.     //deleting items
  50.     for(int k=j; k<nElems; k++)
  51.        arr[k] = arr[k+1];
  52.        nElems--;
  53.  
  54.    //display items
  55.    for(j=0; j<nElems; j++)
  56.        System.out.print(arr[j]+" ");
  57.        System.out.print(" ");
  58.     }
  59. }


    Output





    2. Low Array


DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. /**
  2. * Write a description of class LowArray here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. class LowArray
  8. {
  9.   private long[] a;
  10.   public LowArray(int size){
  11.       a = new long[size];
  12.      }
  13.    public void setElem(int index, long value){
  14.       a[index] = value;
  15.    }
  16.    public long getElem(int index){
  17.       return a[index];
  18.   }
  19. }
  20.  
  21. public class LowArrayApp
  22. {
  23.     public static void main(String [] args){
  24.       // declares an Array
  25.       LowArray arr;
  26.       arr = new LowArray(100);
  27.       int nElems=0;
  28.       int j;

  29.       arr.setElem(0,77);
  30.       arr.setElem(1,99);
  31.       arr.setElem(2,44);
  32.       arr.setElem(3,55);
  33.       arr.setElem(4,22);
  34.       arr.setElem(5,88);
  35.       arr.setElem(6,11);
  36.       arr.setElem(7,66);
  37.       arr.setElem(8,0);
  38.       arr.setElem(9,33);
  39.       nElems = 10;

  40.       //Display items of Array
  41.       for(j=0; j<nElems; j++)
  42.          System.out.print(arr.getElem(j)+ " ");
  43.          System.out.println(" ");

  44.      //Search items
  45.      int searchKey = 26;
  46.      for(j=0; j<nElems; j++)
  47.          if(arr.getElem(j) == searchKey)
  48.          break;

  49.          if(j == nElems)
  50.            System.out.println("Can't find "+ searchKey);
  51.          else
  52.            System.out.println("Found "+ searchKey);
  53.       //delete items
  54.       for(j=0; j<nElems; j++) //search items
  55.           if(arr.getElem(j) == 55)
  56.           break;

  57.       //deleting items
  58.       for(int k=j; k<nElems; k++)
  59.          arr.setElem(k,arr.getElem(k+1));
  60.          nElems--;
  61.      
  62.      //Display items
  63.      for(j=0; j<nElems; j++)
  64.         System.out.print(arr.getElem(j)+" ");
  65.         System.out.println(" ");
  66.     }
  67. }


    Output






    3. Ordered Array

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. * Write a description of class HighArray here.
  2. *
  3. * @author (your name)
  4. * @version (a version number or a date)
  5. */
  6. class HighArray
  7. {
  8.    private long[] a;
  9.    private int nElems;

  10.    public HighArray(int max){
  11.      a = new long[max];
  12.      nElems = 0;
  13.    }

  14.    public boolean find(long searchKey){
  15.       int j;
  16.       for(j=0; j<nElems; j++)
  17.          if(a[j] == searchKey)
  18.          break;

  19.          if(j == nElems)
  20.          return false;
  21.          else
  22.          return true;
  23.     }

  24.    public void insert(long value){
  25.        a[nElems] = value;
  26.        nElems++;
  27. }

  28.    public boolean delete(long value){
  29.        int j;
  30.        for(j=0; j<nElems; j++)
  31.           if(value == a[j])
  32.           break;
  33.   
  34.           if(j == nElems)
  35.           return false;
  36.           else{
  37.        for(int k=j; k<nElems; k++)
  38.           a[k] = a[k+1];
  39.           nElems--;
  40.           return true;
  41.      }
  42. }
  43.    public void display(){
  44.       for(int j=0; j<nElems; j++)
  45.          System.out.print(a[j]+" ");
  46.          System.out.println(" ");
  47.     }
  48. }
  49.    public class HighArrayApp{
  50.    public static void main(String [] args){
  51.       int maxSize = 100;
  52.       HighArray arr;
  53.       arr = new HighArray(maxSize);
  54.  
  55.       arr.insert(77);
  56.       arr.insert(99);
  57.       arr.insert(44);
  58.       arr.insert(55);
  59.       arr.insert(22);
  60.       arr.insert(88);
  61.       arr.insert(11);
  62.       arr.insert(0);
  63.       arr.insert(66);
  64.       arr.insert(33);

  65.       // display items
  66.       arr.display();

  67.      // search items
  68.      int searchKey = 35;
  69.         if(arr.find(searchKey))
  70.            System.out.println("Found "+ searchKey);
  71.         else
  72.            System.out.println("Can't find "+ searchKey);


  73.      // delete items
  74.      arr.delete(0);
  75.      arr.delete(55);
  76.      arr.delete(99);

  77.     // display items
  78.      arr.display();
  79.    }
  80. }


    Output

    4. Class Data Array

DATA HOSTED WITH ♥ BY PASTEBIN.COM - DOWNLOAD RAW - SEE ORIGINAL
  1. /**
  2. * Write a description of class Person here.
  3. *
  4. * @author (your name)
  5. * @version (a version number or a date)
  6. */
  7. class Person
  8. {
  9.     private String lastName;
  10.     private String firstName;
  11.     private int age;

  12.     public Person(String last, String first, int a){
  13.        lastName = last;
  14.        firstName = first;
  15.        age = a;
  16.    }

  17.      public void displayPerson(){
  18.         System.out.println("Last name:"+ lastName);
  19.         System.out.println("First name:"+ firstName);
  20.         System.out.println("Age:"+ age);
  21.     }

  22.      public String getLast(){
  23.        return lastName;
  24.    }
  25. }

  26. class ClassDataArray{
  27.       private Person[] a;
  28.       private int nElems;

  29.       public ClassDataArray(int max){
  30.          a = new Person[max];
  31.          nElems = 0;
  32. }

  33.       public Person find(String searchName){
  34.          int j;
  35.          for(j=0; j<nElems; j++)
  36.             if(a[j].getLast().equals(searchName))
  37.             break;
  38.             if(j == nElems)
  39.             return null;
  40.             else
  41.             return a[j];
  42.    }

  43.       public void insert(String last,String first,int age){
  44.          a[nElems] = new Person(last,first,age);
  45.          nElems++;
  46. }

  47.       public boolean delete(String searchName){
  48.          int j;
  49.          for(j=0; j<nElems; j++)
  50.             if(a[j].getLast().equals(searchName))
  51.             break;

  52.             if(j == nElems)
  53.             return false;
  54.             else{
  55.          for(int k=j; k<nElems; k++)
  56.             a[k] = a[k+1];
  57.             nElems--;
  58.             return true;
  59.      }
  60. }

  61.      public void displayA(){
  62.         for(int j=0; j<nElems; j++)
  63.            a[j].displayPerson();
  64.      }
  65. }

  66.      public class ClassDataApp{
  67.      public static void main(String[] args){
  68.         int maxSize = 100;
  69.      ClassDataArray arr;
  70.         arr = new ClassDataArray(maxSize);

  71.        //insert items
  72.        arr.insert("Evans", "Patty", 24);
  73.        arr.insert("Smith", "Loraine", 37);
  74.        arr.insert("Yee", "Tom", 43);
  75.        arr.insert("Adams", "Henry", 63);
  76.        arr.insert("Hashimoto", "Sato", 21);
  77.        arr.insert("Stimson", "Henry", 29);
  78.        arr.insert("Velasquez", "Jose", 72);
  79.        arr.insert("Lamarque", "Henry", 54);
  80.        arr.insert("Vang", "Minh", 22);
  81.        arr.insert("Creswell", "Lucinda", 18);


  82.        //display items
  83.        arr.displayA();

  84.       //search items
  85.        String searchKey = "Stimson";
  86.           Person found;
  87.           found = arr.find(searchKey); 
  88.              if(found !=null){
  89.                 System.out.println("Found ");
  90.           found.displayPerson();
  91.    }
  92.              else
  93.                 System.out.println("Can't find "+searchKey);
  94.  
  95.            //delete items
  96.                 System.out.println("Deleting Smith, Yee, and Creswell");
  97.             arr.delete("Smith");
  98.             arr.delete("Yee");
  99.             arr.delete("Creswell");

  100.           //display items
  101.           arr.displayA();
  102.      }
  103. }

    Output



Komentar

Postingan populer dari blog ini

KUIS Akhir EPL

Tower of Hanoi - Java

Binary Search Tree