0

I have the following question.

int[] ar={4,6,7,8}

Now i want to add an element so i get

ar={4,6,9,7,8}

I want to be able to add a given element (9) in a position that i want i this case index 2. How?

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
mr_ecko
  • 61
  • 1
  • 1
  • 6
  • 3
    An array's length is **final**. Which means if you want to add or remove elements, you need to create a new array and copy everything along with new element. – Codebender Jul 19 '15 at 15:34
  • Ok, so there is no simple method. i have to use Arraylist.But then how to convert my ar to an arrylist.? – mr_ecko Jul 19 '15 at 15:46
  • there are API's existing to convert array's to list. Like `Arrays.asList()`, but they will give **fixed sized list** which means you cant add or remove from them as well. So you need to copy the list to a new one with `new ArrayList(Arrays.asList(arr))`. Or you could use [`System.arrayCopy()`](http://docs.oracle.com/javase/7/docs/api/java/lang/System.html) and copy from original array to your new array. – Codebender Jul 19 '15 at 16:33

4 Answers4

3

In Java, the size of an array cannot be changed. Instead you can use a List:

List<Integer> numList = new ArrayList<Integer>();
numList.add(4);
numList.add(6);
numList.add(7);
numList.add(8);

Then you can use numList.add(int index, E element); to insert values at specific positions.

numList.add(2, 9);
//numList = {4, 6, 9, 7, 8};

For further information you may want to look at this tutorial.

deezy
  • 1,480
  • 1
  • 11
  • 22
  • Use a `List` and... then do what? (You should perhaps say to use `List.insert`). – Andy Turner Jul 19 '15 at 15:35
  • @AndyTurner I added links to the Javadoc and an Oracle tutorial. – Turing85 Jul 19 '15 at 15:37
  • @Turing85 but you've not mentioned what to look for in the javadoc or tutorial. It would be a better answer to say explicitly that `List.insert` is the pertinent method. – Andy Turner Jul 19 '15 at 15:38
  • exactly, just giving a tutorial dont cut it. Those are big and i dont know where to look for :-( still thx. – mr_ecko Jul 19 '15 at 15:44
  • @mr_ecko there is a subsection called "The List Interface", which explains the most basic stuff about `List`s. Who am I to decide what is "important" to you and what is not? Therefore I like to give more general information and let you decide what to pick. The subpage is maybe five printed pages long. – Turing85 Jul 19 '15 at 15:48
0

Java arrays are fixed length, so you'll need to create another one to store your extra element.

int[] ar = { 4, 6, 7, 8 };
int[] tmp = new int[ar.length + 1];
int pos = 2;
for (int i = 0; i < pos; i++) {
    tmp[i] = ar[i];
}
for (int i = pos + 1; i <= ar.length; i++) {
    tmp[i] = ar[i - 1];
}
tmp[pos] = 9;
System.out.println(Arrays.toString(tmp));

Output is (as requested)

[4, 6, 9, 7, 8]
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • Just for documentation: when you ever need to copy arrays within productive code, please use [`System.arraycopy(...)`](https://docs.oracle.com/javase/8/docs/api/java/lang/System.html#arraycopy-java.lang.Object-int-java.lang.Object-int-int-) (I am pretty sure Elliott Frisch knows this). – Turing85 Jul 19 '15 at 15:40
0

How to add new elements to an array?

add an element to int [] array in java

Note that ArrayList also has an add method that lets you specify an index and the element to be added void add(int index, E element).

Community
  • 1
  • 1
mjalkio
  • 78
  • 7
0

The most simple way of doing this is to use an ArrayList<Integer> and use the add(int, T) method.

List<Integer> numList = new ArrayList<Integer>();
numList.add(4);
numList.add(6);

// Now, we will insert the number
numList.add(2, 9);
Josh Stevens
  • 3,943
  • 1
  • 15
  • 22