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
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started