1

I have a read a lot of posts here regarding inserting a new element in an array but my doubts have not been cleared. I did some more research and came across System.arraycopy() method to do it. But I somehow feel that it's not efficient. Is there any other way I can do this? Converting the array into an arraylist seems to be confusing as when I tried the Arrays.asList(), I couldn't add elements to it.

Here is my code that I wrote using System.arraycopy():

import java.util.Arrays;
class example{

    public static void main(String[] args) {
        int[] array = new int[]{2,3,4,5,6,7};
        int[] newarray = new int[7];

        int indexvalue = 2;
        int num = 8;

        System.arraycopy(array, 0, newarray, 0, 2);
        System.out.println(newarray[2]=8);
        System.arraycopy(array, 2, newarray, 3,4);
           System.out.println(Arrays.toString(newarray));
     }
}

and it gives this output :

run: 8 [2, 3, 8, 4, 5, 6, 7]

BUILD SUCCESSFUL (total time: 0 seconds)

Is there a better way to do this?

EDIT : The reason I am using arrays for this is because the question asks me to insert a new element in an array.

Thor0o0
  • 367
  • 1
  • 4
  • 17
  • Just don't use array, if you are going to change its length – maszter Nov 02 '16 at 19:22
  • What output did you expect? – bradimus Nov 02 '16 at 19:22
  • @maszter: I understand but I am doing these questions online and one of them is "Write a Java program to insert an element (specific position) into an array." – Thor0o0 Nov 02 '16 at 19:29
  • In general, arrays are not re-sizeable and a new buffer is required when you want to change their length. You are correct, this is not very efficient. Options are: Use a dynamic structure like ArrayList or create the array with "extra" space and fill it in as you go along. – ebyrob Nov 02 '16 at 19:36
  • @ebyrob Yes, that is something I could learn about. Thank you, really. – Thor0o0 Nov 02 '16 at 19:40

1 Answers1

1

Given that you want to dynamically add elements, a List is probably the right data structure to use, not an array. You could do it like this:

class example{
    public static void main(String[] args) {
        ArrayList<Integer> array = new ArrayList<>();
        array.add(2);
        array.add(3);
        array.add(4);
        array.add(5);
        array.add(6);
        array.add(7);

        int indexvalue = 2;
        Integer num = 8;

        array.add(indexvalue, num);
    }
}

Here is the javadoc on the add method I used here to insert the element: http://docs.oracle.com/javase/6/docs/api/java/util/List.html#add%28int,%20E%29

See this post for the many reasons a list is better than an array for what you are trying to do: Array versus List<T>: When to use which?

Community
  • 1
  • 1
nhouser9
  • 6,730
  • 3
  • 21
  • 42
  • Thank you. I have a doubt though. Would I have to convert the arraylist to an array? – Thor0o0 Nov 02 '16 at 19:30
  • @Arjun0o That depends what you want to do with it. There should be no reason you have to convert it into an array. However, instead of getting elements like this: `array[2]` you would get them like this: `array.get(2)`. I suggest you try it yourself! Please remember to upvote and accept if this was helpful – nhouser9 Nov 02 '16 at 19:32