fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. static int[] array = new int[10];
  11. static int size = 0;
  12. public static void main (String[] args) throws java.lang.Exception
  13. {
  14. init();
  15. insert(3,1);
  16. System.out.println(Arrays.toString(array));
  17. delete(3);
  18. System.out.println(Arrays.toString(array));
  19. }
  20. public static void init(){
  21. Random r = new Random();
  22. for(int i = 0 ; i < 10 ; i ++){
  23. array[i] = r.nextInt(11);
  24. size ++;
  25. }
  26. System.out.println(Arrays.toString(array));
  27. }
  28. public static void insert(int index,int element){
  29. if(array.length == size){
  30. array = Arrays.copyOf(array,array.length + 1);
  31. }
  32. for(int i = size; i > index ; i--){
  33. array[i] = array[i - 1];
  34. }
  35. array[index] = element;
  36. }
  37. public static void delete(int index){
  38. if(index > array.length - 1){
  39. System.out.println("超过数组范围");
  40. }
  41. for(int i = index; i < array.length - 1;i++){
  42. array[i] = array[i+1];
  43. }
  44. size --;
  45. array = Arrays.copyOf(array,array.length-1);
  46. }
  47. }
Success #stdin #stdout 0.08s 47136KB
stdin
Standard input is empty
stdout
[7, 2, 9, 9, 0, 8, 0, 3, 6, 0]
[7, 2, 9, 1, 9, 0, 8, 0, 3, 6, 0]
[7, 2, 9, 9, 0, 8, 0, 3, 6, 0]