1

So, the problem is a bit advanced for a beginner like myself which is why I hope you can ignore my mistakes.

We need to create a method that takes an array, a value that I want to place in the array and the index values (spaces) in the array.

Because my teacher was not concrete with what she wanted, she didn't specify if the array had to be empty or full which is what has confused me the most in this exercise.

The static method has to be named:

addAtIndex( int [] a, int value, int index) 

and do the following:

  • If the last space in the array is 0 or empty and I want to place a new integer value in one of the already occupied spaces, I need to move every element to the right and leave that space free in order to be able to do that.

  • If the last space has already been taken by an integer value, I need to "resize" the array and make more space so that I can keep entering more integer values. I know that you cannot resize an array and even less keep all the elements intact so I need to create a larger array to do that.

I have been programming in Java less than two months so I find this quite difficult.

I have been able to create an empty array, and I have entered different numerical values in the array with the given parameters. All of this has been done in public static void main and not in a method. I can't seem to be able to do this in a method, especially I have no idea how I can move everything in an array to the right.

import java.util.Arrays;
import java.util.Scanner;

public class Array{
  public static void main (String args []){

     Scanner input = new Scanner(System.in);
     int [] array   = new int[10];

    for (int x = 0; x <= 9; x++){
     int index = input.nextInt();
     int value    = (int)(Math.random()*50);

      //move elements below insertion point.
    for (int i = array.length-1; i > index; i--){
        array[i] = array[i-1];
       }
       //insert new value
        array[index] = value;
      }
      System.out.println(Arrays.toString(array));
   }
}

This of course is way off from what the teacher wants me to do. What I'm doing here is just creating an array, entering random integers in the indexes that I choose and printing it out. I need some help.

2
  • Updated with examples and a bit of refactoring. Commented Mar 31, 2019 at 21:47
  • Btw, for any doubt, just ask. Commented Mar 31, 2019 at 22:24

1 Answer 1

1

That's how I'd do it in the most minimalistic way.
You might need to adjust because I didn't spend that much time on it.
However it should give you a big boost.

Remember an int[] array cannot have nulls. So I've taken an "empty" value to be zero.
Follow the comments!

static int[] addAtIndex(
        final int[] array,
        final int value,
        final int index) {
    if (array == null || array.length == 0) {
        // We cannot do anything.
        // Simply return to the caller
        return array;
    }

    // This is the array reference holder
    int[] localArray = array;

    // Get the last element of the array
    final int last = localArray[localArray.length - 1];

    // Is the last element 0? Remember an int array cannot contain nulls
    if (last == 0) {
        if (array[index] != 0) {
            // We need to shift everything to the right
            // to give space to the new element
            shiftRight(localArray, index);
        }
    } else {
        // Create a bigger array of length = actualLength + 1
        // and assign it to the reference holder
        localArray = new int[array.length + 1];

        // Copy the array elements to the new array, leaving a space
        // for the new element
        copyArrayAndLeaveSpace(index, array, localArray);
    }

    // Assign the new value
    localArray[index] = value;

    // Return the modified array reference
    return localArray;
}

static void shiftRight(
        final int[] array,
        final int index) {
    System.arraycopy(array, index, array, index + 1, array.length - index - 1);
}

static void copyArrayAndLeaveSpace(
        final int index,
        final int[] array,
        final int[] newArray) {
    System.arraycopy(array, 0, newArray, 0, index);
    System.arraycopy(array, index, newArray, index + 1, array.length - index);
}

Usage

final int[] nums = {1, 2, 3, 4, 5, 6, 8, 9, 0};
final int[] result = addAtIndex(nums, 7, 4);

Output: [1, 2, 3, 4, 7, 5, 6, 8, 9]


final int[] nums = {1, 2, 3, 4, 5, 6, 8, 9, 1};
final int[] result = addAtIndex(nums, 7, 4);

Output: [1, 2, 3, 4, 7, 5, 6, 8, 9, 1]

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.