Advertisement
kamandos

HW2

Jun 29th, 2022
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.57 KB | None | 0 0
  1. import java.util.Arrays;
  2.  
  3. class Main
  4. {
  5.     private static int[] insert(int[] a, int key, int index)
  6.     {
  7.         int[] result = new int[a.length + 1];
  8.  
  9.         System.arraycopy(a, 0, result, 0, index);
  10.         result[index] = key;
  11.         System.arraycopy(a, index, result, index + 1, a.length - index);
  12.  
  13.         return result;
  14.     }
  15.  
  16.     public static void main(String[] args)
  17.     {
  18.         int[] a = { 1, 2, 4, 5 };
  19.         int key =  3;
  20.         int index = 2;
  21.  
  22.         a = insert(a, key, index);
  23.         System.out.println(Arrays.toString(a));
  24.     }
  25.  
  26. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement