afiqakraam

Untitled

Mar 26th, 2021
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.04 KB | None | 0 0
  1.  
  2. /**
  3.  * HighArray aa
  4.  *
  5.  * @afiqakraam
  6.  * @24/03/2021
  7.  */
  8. class HighArray
  9. {
  10.     private long[] a;
  11.     private int nElems;
  12.    
  13.     public HighArray(int max){
  14.         a = new long[max];
  15.         nElems = 0;
  16.     }
  17.    
  18.     public boolean find(long searchKey){
  19.         int j;
  20.         for(j = 0; j <nElems; j++)
  21.             if(a[j] == searchKey)
  22.             break;
  23.            
  24.         if(j == nElems)
  25.             return false;
  26.         else
  27.             return true;
  28.         }
  29.        
  30.     public void insert(long value){
  31.         a[nElems] = value;
  32.         nElems++;
  33.     }
  34.    
  35.     public boolean delete(long value){
  36.         int j;
  37.         for(j = 0; j < nElems; j++)
  38.             if(value == a[j])
  39.                 break;
  40.                
  41.             if(j == nElems)
  42.                 return false;
  43.             else{
  44.                 for(int k=j; k < nElems; k++)
  45.                     a[k] = a[k+1];
  46.                     nElems--;
  47.                     return true;
  48.                 }
  49.             }
  50.            
  51.     public void display(){
  52.         for(int j = 0; j < nElems; j++)
  53.             System.out.print(a[j]+" ");
  54.         System.out.println(" ");
  55.     }
  56. }
  57. public class HighArrayApp{
  58.     public static void main(String[] args){
  59.         int maxSize = 100;
  60.         HighArray arr;
  61.         arr = new HighArray(maxSize);
  62.        
  63.         arr.insert(77);
  64.         arr.insert(99);
  65.         arr.insert(44);
  66.         arr.insert(55);
  67.         arr.insert(22);
  68.         arr.insert(88);
  69.         arr.insert(11);
  70.         arr.insert(0);
  71.         arr.insert(66);
  72.         arr.insert(33);
  73.        
  74.         arr.display();
  75.        
  76.         //Search Element
  77.         int searchKey = 35;
  78.         if(arr.find(searchKey))
  79.             System.out.println("Found "+searchKey);
  80.         else
  81.             System.out.println("Can't find "+searchKey);
  82.            
  83.            
  84.         //Delete Elements
  85.         arr.delete(0);
  86.         arr.delete(55);
  87.         arr.delete(99);
  88.        
  89.         //Display Elements
  90.         arr.display();
  91.     }
  92. }
  93.  
  94.  
Advertisement
Add Comment
Please, Sign In to add comment