Advertisement
Vladkoheca

ArraysElementRemoveorAdd.java

Nov 19th, 2024
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.63 KB | Source Code | 0 0
  1. import java.util.Arrays;
  2. import java.util.Scanner;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) {
  6.  
  7.         Scanner sc = new Scanner(System.in);
  8.  
  9.         int[] massive = {1, 2, 3, 4, 5};
  10.         int elementToAdd = 99;
  11.         int indexToInsert = 2;
  12.  
  13.         System.out.println(Arrays.toString(massive));
  14.  
  15.        // massive = Arrays.copyOfRange(massive, 1, massive.length);
  16.  
  17.         for (int i = massive.length - 1; i > indexToInsert ; i--) {
  18.             massive[i] = massive[i-1];
  19.         }
  20.  
  21.         massive[indexToInsert] = elementToAdd;
  22.         System.out.println(Arrays.toString(massive));
  23.  
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement