fork(2) 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. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. int newNumber = 2;
  13. int newIndex=1;
  14. int[] gato={1,4,3,7,8}; //old array
  15. int[] perro = new int[gato.length+1]; //new array
  16.  
  17. System.out.print("Old array: ");
  18. for(int k = 0; k<gato.length; k++){
  19. System.out.print(gato[k] + ", ");
  20. }
  21. System.out.println();
  22.  
  23. int j = 0;
  24. for(int i = 0; i<perro.length; i++){
  25. if(i == newIndex){
  26. perro[i] = newNumber;
  27. }
  28. else{
  29. perro[i] = gato[j];
  30. j++;
  31. }
  32. }
  33.  
  34. System.out.print("New array: ");
  35. for(int k = 0; k<perro.length; k++){
  36. System.out.print(perro[k] + ", ");
  37. }
  38. System.out.println();
  39.  
  40. }
  41. }
Success #stdin #stdout 0.09s 320320KB
stdin
Standard input is empty
stdout
Old array: 1, 4, 3, 7, 8, 
New array: 1, 2, 4, 3, 7, 8,