0

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;
4
  • 3
    Hi , I think you should use something like a double ended queue data structure or similar. Commented Jan 14, 2021 at 16:45
  • 1
    A convenient class for manipulating the size of an array (adding and removing elements regardless of position) is ArrayList. Using an array of integers is not nearly as convenient as ArrayList<Integer>. Commented Jan 14, 2021 at 16:54
  • b[0]=4 this way you can add value at first Commented Jan 14, 2021 at 16:56
  • 1
    Arrays, in Java, are allocated in the memory as a fixed/static size chunk, and they never change. It seems like you want to shift your array one place right, set the index 0 free, and then add something there.. and there is no other way for this, than to copy each element one by one, starting from index 1 of the destination array. Commented Jan 14, 2021 at 16:57

4 Answers 4

1

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));
}
Sign up to request clarification or add additional context in comments.

Comments

0

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;

Comments

0

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;
    

Comments

0

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);
    }
}

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.