Question: This is our question: Write a method that takes an array of ints,an integer value,and an integer index.The method should insert the value at the

This is our question:

Write a method that takes an array of ints,an integer value,and an integer index.The method should insert the value at the given index and move the values afterwards by one,losing the last value.This method must be called insertValue() and must have the following parameter types:int[],integer,integer.

For example:if a = {1,2,3,4,5},insertValue(a,100,2)would change the array {1,2,3,4,5} to {1,2,100,3,4}

This is our code so far:

import java.io.*; import java.util.Scanner; import java.util.Arrays; public class insertValue { public static void main(String args[]) { int[] a = {1,2,3,4,5}; insertValue(a,100,2); System.out.println(Arrays.toString(a)); } public static void insertValue(int[] a,int random,int index) { for(int i = 0;i < a.length -1;i++) { if(index == i) { a[i+1] = a[i]; a[i] = random; } } } }

our Output:[1, 2, 100, 3, 5]

We have been able to get most of the question right,but we are stuck on how to move the values afterwards by one,losing the last value.

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!