Advertisement
Samuel_Berkat_Hulu

ArrayApp

Mar 29th, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.66 KB | None | 0 0
  1. /**
  2.  * Write a description of class ArrayApp here.
  3.  *
  4.  * @Samuel Berkat Hulu
  5.  * @version 5.0
  6.  */
  7. public class ArrayApp
  8. {
  9.     public static void main(String[] args)
  10.     {
  11.         //Inisialisasi Array
  12.         long[] arr;
  13.         arr = new long[100];
  14.         int nelems = 0;
  15.         int j;
  16.         long searchKey;
  17.        
  18.             //Inisialisasi Arry
  19.             arr[0] = 77;
  20.             arr[1] = 99;
  21.             arr[2] = 44;
  22.             arr[3] = 55;
  23.             arr[4] = 22;
  24.             arr[5] = 88;
  25.             arr[6] = 11;
  26.             arr[7] = 0;
  27.             arr[8] = 66;
  28.             arr[9] = 33;
  29.             nelems = 10;
  30.        
  31.         //Menampilkan Elemen
  32.             for(j=0; j<nelems; j++)
  33.                 System.out.print(arr[j] + " ");
  34.                 System.out.println("");
  35.            
  36.             //Mencari  Element dengan kunci 66
  37.             searchKey = 66;
  38.             for(j=0; j<nelems; j++)
  39.                 if(arr[j] == searchKey)
  40.                 break;
  41.                 if(j == nelems)
  42.                     System.out.println("Tidak dapat menemukan " + searchKey);
  43.                 else
  44.                     System.out.println("Di Temukan " + searchKey);
  45.            
  46.             //Hapus element dengan kunci 55
  47.             searchKey = 55;
  48.             for(j=0; j<nelems; j++)
  49.                 if(arr[j] == searchKey)
  50.                 break;
  51.             for(int k=j; k<nelems; k++)
  52.                 arr[k] = arr[k+1];
  53.                 nelems--;
  54.            
  55.             //Menampilkan Array
  56.             for(j=0; j<nelems; j++)
  57.                 System.out.print( arr[j] + " ");
  58.                 System.out.println("");
  59.    
  60.     }
  61. }
  62.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement