Remove Array Element

Run Settings
LanguageJava
Language Version
Run Command
import java.util.Arrays; class Main { public static void main(String[] args) { //create an array with 10 elements int[] intArray = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100}; //remove element "40" int target=3; //create a copy to hold the new values (1 shorter of course) int[] copy = new int[intArray.length - 1]; for (int i = 0, j = 0; i < intArray.length; i++) { //begin i and j at 0 if (i != target) { //copy everything as long as i doesnt match target copy[j++] = intArray[i]; //fill position of j with element of i } } //print entire array without for loop (must import java.util.Arrays) System.out.println("Original" + Arrays.toString(intArray));//original System.out.println("Modified" + Arrays.toString(copy)); //modified //Yes, you can do it again following the same principals... int[] copy2 = new int[copy.length - 1]; int target2 = 2; for (int i = 0, j = 0; i < copy.length; i++) { if (i != target2) { copy2[j++] = copy[i]; } } System.out.println("Original" + Arrays.toString(copy)); System.out.println("Modified" + Arrays.toString(copy2)); } }
Editor Settings
Theme
Key bindings
Full width
Lines