SY K
SY K

Reputation: 1

How to add number in beginning in array java

In array, How do we put new integers in the beginning? I know how to add in the last. But can anyone teach me how to add in the front of the array?

 int[]b=new int[a.length+1];
 for (int i=0; i<a.length;i++) {
     b[i]=a[i];
 }
 b[a.length]=0;
 return b;

Upvotes: 0

Views: 833

Answers (4)

ahrooran
ahrooran

Reputation: 1115

For this answer I assume that your ARRAY has a blank box (free position) to spare for the new element. Then it will be very easy.

System.arraycopy(yourArray, 0, yourArray, 1, yourArray.length - 1);
yourArray[0] = newElement;

You don't need a temp array if yourArray can have an additional element. arraycopy() can do all the hardwork of creating temp array, copying values, etc. for you. Make sure BOTH srcPos and destPos are same which is yourArray

Here is the full demo:

public static void main(String[] args) {

    int[] yourArray = new int[5];
    Arrays.fill(yourArray, 0, 4, 1);
    System.out.println("Assume your array looks like this (with additional blank box for new element): " + Arrays.toString(yourArray));

    int newElement = 5000;
    System.arraycopy(yourArray, 0, yourArray, 1, yourArray.length - 1);
    yourArray[0] = newElement;

    System.out.println(Arrays.toString(yourArray));
}

If in case you DON'T have any extra space in your array already, you need another array as a placeholder with extra space (finalArray). Instead of having same array in both places, just change destPos to finalArray.

Here is the demo for that:

public static void main(String[] args) {

    int[] yourArray = new int[5];
    Arrays.fill(yourArray, 0, 5, 1);
    System.out.println("Assume your array looks like this (withOUT additional blank box for new element): " + Arrays.toString(yourArray));

    int newElement = 5000;
    int[] finalArray = new int[yourArray.length + 1];
    System.arraycopy(yourArray, 0, finalArray, 1, yourArray.length);
    finalArray[0] = newElement;

    System.out.println(Arrays.toString(finalArray));
}

Upvotes: 1

Joby Wilson Mathews
Joby Wilson Mathews

Reputation: 11116

You need to create a temporary array ,assign the first position with new element and then copy all the elements in the old array to the new temporary array.

Example:

import java.util.Arrays;

public class Test
{
    public static void main(String[] args) {
        int[] arrayElements = new int[]{1,2,3,4,5,6,7};
        int newElement=0;
        int[] tempArr = new int[arrayElements.length + 1];
        tempArr[0] = newElement;
        System.arraycopy(arrayElements, 0, tempArr, 1, arrayElements.length);
        Arrays.stream(tempArr).forEach(System.out::println);
    }
}

Upvotes: 0

csilvano
csilvano

Reputation: 23

It is almost the same operation, reversed and with a simple trick.

  1. You want to add a new element, so you must create an array big enough to host it, as you did, setting its length as 1 + a.length.

  2. Then you may set the first element to the value you desire to add, I'll write MY_VALUE.

  3. Now you want to fill up the array: this means you have to iterate from index 1 (as 0 has already been filled with MY_VALUE) until we reach b.length - 1.

  4. While iterating, remember that b is bigger than a, plus you're starting with index i = 1, so while this is good for b, you need to adjust it for a, so you'll have to iterate over it "one step less". To do this, you simply have to access all i-1 elements from a, as you can see from the code below.

     int[] b = new int[a.length+1];
     b[0] = 0;
    
     for (int i = 1; i < b.length; i++) {
         b[i] = a[i-1];
     }
    
     return b;
    

Upvotes: 0

deamonLucy
deamonLucy

Reputation: 205

If you use an array you can only add elements based on the position. If you want to add a new element to the beginning then you need to shift all other elements one position to the right. Also, keep in mind that you can not change the size of the array. The sifting would look something like this. N being the position you are placing the new element to.

     temp=a[n+1];
     for(i=n+1;i>a.length-1;i++)
     {
       a[i]=a[i+1];
     }
     a[a.length-1]=temp;

Upvotes: 0

Related Questions