fork(1) download
  1. class ArrayApp
  2. {
  3. public static void main(String[] args)
  4. {
  5. long[] arr; // reference to array
  6. arr = new long[100]; // make array
  7. int nElems = 0; // number of items
  8. int j; // loop counter
  9. long searchKey; // key of item to search for
  10. //--------------------------------------------------------------
  11. arr[0] = 77; // insert 10 items
  12. arr[1] = 99;
  13. arr[2] = 44;
  14. arr[3] = 55;
  15. arr[4] = 22;
  16. arr[5] = 88;
  17. arr[6] = 11;
  18. arr[7] = 00;
  19. arr[8] = 66;
  20. arr[9] = 33;
  21. nElems = 10; // now 10 items in array
  22. //--------------------------------------------------------------
  23. for(j=0; j<nElems; j++) // display items
  24. System.out.print(arr[j] + " ");
  25. System.out.println("");
  26. //--------------------------------------------------------------
  27. searchKey = 66; // find item with key 66
  28. for(j=0; j<nElems; j++) // for each element,
  29. if(arr[j] == searchKey) // found item?
  30. break; // yes, exit before end
  31. if(j == nElems) // at the end?
  32. System.out.println("Can't find " + searchKey); // yes
  33. else
  34. System.out.println("Found " + searchKey); // no
  35. //--------------------------------------------------------------
  36. searchKey = 55; // delete item with key 55
  37. for(j=0; j<nElems; j++) // look for it
  38. if(arr[j] == searchKey)
  39. break;
  40. for(int k=j; k<nElems; k++) // move higher ones down
  41. arr[k] = arr[k+1];
  42. nElems--; // decrement size
  43. //--------------------------------------------------------------
  44. for(j=0; j<nElems; j++) // display items
  45. System.out.print( arr[j] + " ");
  46. System.out.println("");
  47. } // end main()
  48. } // end class ArrayApp
Success #stdin #stdout 0.17s 57464KB
stdin
Standard input is empty
stdout
77 99 44 55 22 88 11 0 66 33 
Found 66
77 99 44 22 88 11 0 66 33