Home > Blockchain >  I want to add an element to an array at specific position without reducing its size, i tried this bu
I want to add an element to an array at specific position without reducing its size, i tried this bu

Time:10-12

import java.util.Scanner;

public class AddElementToSpecificPosition {

    private static  Scanner scanner = new Scanner(System.in);


    public static void main(String[] args) {

        int[]  array =arrayDetails();
        scanner.nextLine();
        System.out.println("Enter the position for the element \r");
        int position = scanner.nextInt();
        System.out.println("Enter the  element \r");
        int element = scanner.nextInt();
        addElementToSpecificPositoin(array,position,element);

    }

    //getting details form user for arry
    private static int [] arrayDetails() {

        System.out.println("Enter the length for the array \r");
        int length = scanner.nextInt();
        scanner.nextLine();
        int [] intArray =  new int[length];
        System.out.println("Enter the numbers for the array \r");
        for(int i=0; i<intArray.length; i  ) {
            intArray[i] = scanner.nextInt();
        }
        return intArray;
    }

    //trying to add an element to specific position

    private static void addElementToSpecificPositoin(int[] array, int position, int element) {
        int lastIndexValue = array[array.length-1];
        
        for (int i=array.length-1; i>position-1 ; i-- ) {
            array[i]=array[i-1];
        }
        array[position-1] = element;
        int addedPosition = array.length ;
        int [] newArray = new int[addedPosition 1];
        for (int i =0; i<newArray.length; i  ) {
            newArray[i] = array[i]; 
        }
        
        newArray[addedPosition] = lastIndexValue;
        
        for (int j =0; j<newArray.length; j  ) {
            System.out.println(newArray[j]);
        }


    }

}

CodePudding user response:

array is size X. Whatever it might be, let's call it X. Then you do:

int addedPosition = array.length;
int[] newArray = new int[addedPosition   1];

In other words, newArray has size X 1. You then loop through 0 through newArray.length and resolve array[i] for each i. This, of course, means array[X] is resolved which is a non-existing entry.

Instead of asking on SO, you should invest a little bit of time and learn to debug. It's easy! All you really do is figure out what a line of code should be doing (by just looking at it and basically 'being the computer'. Use pen and paper if you prefer), then run the code line by line and check that the code actually does what you think it should.

If, thinking it through line by line and working it out, you realize the code isn't what you wanted: Great, you found a bug. Fix it and keep applying this process. Otherwise, you'll figure it out when what the code actually does, does not match what you thought it would. Debuggers help a ton when doing this, but a boatload of System.out.println statements can help, too. You'd have figured it out once you realize with e.g. an example input of a 4-size array ends up running newArray[4] = array[4].

CodePudding user response:

Arrays are fixed sized, and java is pass-by-value. Passing an array variable will never assign to that variable.

private static int[] insert(int[] array, int position, int element) {
   int[] largerArray = Arrays.copyOf(array, array.length   1);
   System.arraycopy(largerArray, position, largerArray, position   1,
       array.length - position);
   largerArray[position] = element;
   return largerArray;
}

array = insert(array, 13, 42);

The above uses a class Arrays with nice utility functions. And System.arraycopy is a fast function to copy array slices. It also deals with overlapping slices.

You need to assign to the passed in array variable the resulting larger array.

  • Related