Question: This is my teacher's code and i have a hard time understanding how to change values in arrays. Can you explain what's happening in the

This is my teacher's code and i have a hard time understanding how to change values in arrays. Can you explain what's happening in the code? Thanks.

import java.util.Arrays;

class Main {

public static void insert(int value, int numberOfItems, int[] a) {

int [] random = new int[numberOfItems+1];

for(int i=0;i

random[i] = a[i];

}

random[numberOfItems] = value;

a = random;

System.out.println(Arrays.toString(a));

}

public static void delete(int value, int numberOfItems, int[] a) {

boolean statement = true;

int random[] = new int[numberOfItems-1];

int z = 0;

for(int i=0;i

if(statement && value == a[i]) {

statement = false;

continue;

}

random[z] = a[i];

z++;

}

a = random;

System.out.println(Arrays.toString(a));

}

public static void deleteAll(int value, int numberOfItems, int[] a) {

int count = 0,z=0;

int temp[] = new int[numberOfItems];

for(int i=0;i

if(value == a[i]) {

count++;

continue;

}

temp[z] = a[i];

z++;

}

int anotherRandom[] = new int[numberOfItems-count];

for(int i=0;i

anotherRandom[i] = temp[i];

}

a = anotherRandom;

System.out.println(Arrays.toString(a));

}

public static void main(String[] args) {

int a [] = {1,3,5,0,0,0,0,0,0,0};

Main.insert(4, 3, a);

Main.delete(5,3,a);

Main.deleteAll(1,4,a);

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!