Advertisement
Guest User

Untitled

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