Guest User

Untitled

a guest
Mar 28th, 2021
51
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  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. }
  60.  
Advertisement
Add Comment
Please, Sign In to add comment