Pages

Footer Pages

Spring Boot

Java String API

Java Conversions

Kotlin Programs

Kotlin Conversions

Java Threads Tutorial

Java 8 Tutorial

Friday, December 17, 2021

Java Array Insert - Add Values At The Specific Index

1. Overview

In this tutorial, We'll learn how to insert an element at the specific index for the given array in java.

if you are new to java, Please read the below articles on arrays.



This can be done in two ways. Let us write the example code to insert the value at any given position.

All examples shown in this article are present in GitHub and it is provided at the end of the article.

Java Array Insert - Add Values At The Specific Index




2. Java Insert Value into Array Using New Array


In this approach, first, we will create the new array with the original array size + 1.
Next, iterate the array and copy each index value into the new array until the given index. 

after that, store the index value in the temp variable where the new value has to be inserted. Then, insert the given value in the given index.

Next, insert the rest of the elements to the new array from the old array.

Example
package com.javaprogramto.arrays.insert;

import java.util.Arrays;

public class JavaArrayInsertExample1 {

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

		System.out.println("orininal array - " + Arrays.toString(array));
		int index = 5;
		int value = 10;

		int[] newArray = insertArray(array, index, value);
		System.out.println("New array after insertion - " + Arrays.toString(newArray));

	}

	private static int[] insertArray(int[] array, int index, int value) {

		int length = array.length;

		if (index > length) {
			throw new RuntimeException("Invalid index - " + index);
		}

		int[] newArray = new int[length + 1];

		int temp = 0;

		for (int i = 0; i < length; i++) {

			if (i <= index - 1) {
				newArray[i] = array[i];
			} else if (i == index) {
				temp = array[i];
				newArray[index] = value;
			} else {
				newArray[i] = temp;
				temp = array[i];
			}

		}

		newArray[newArray.length - 1] = temp;

		return newArray;
	}

}

Output
orininal array - [1, 2, 3, 4, 5, 6, 7]
New array after insertion - [1, 2, 3, 4, 5, 10, 6, 7]

3. Java Insert Value into Array Using New Array Without Temp Variable


Another way is without using temp variable.

In the below code, we have used the original and new array indexes and copied all the values from original to new one from index 0 to size + 1.

Example 2
package com.javaprogramto.arrays.insert;

import java.util.Arrays;

public class JavaArrayInsertExample2 {

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

		System.out.println("orininal array - " + Arrays.toString(array));
		int index = 5;
		int value = 10;

		int[] newArray = insertArray(array, index, value);
		System.out.println("New array after insertion - " + Arrays.toString(newArray));

	}

	private static int[] insertArray(int[] array, int index, int value) {

		int length = array.length;

		if (index > length) {
			throw new RuntimeException("Invalid index - " + index);
		}

		int[] newArray = new int[length + 1];

		for (int i = 0; i < length + 1; i++) {

			if (i < index - 1) {
				newArray[i] = array[i];
			} else if (i == index-1) {
				newArray[index] = value;
			} else {
				newArray[i] = array[i - 1];
			}

		}

		return newArray;
	}

}

Output
orininal array - [1, 2, 3, 4, 5, 6, 7]
New array after insertion - [1, 2, 3, 4, 5, 10, 6, 7]

4. Java Array Array Using ArrayList


In this approach, we no need to run the loop to copy from the old array to the new array. Because, we are going to use the ArrayList add() method to insert into. add() method shifts all values right to the given index.


Example 3

This example code is to work with the Integer wrapper array.

 private static Integer[] insertArray(Integer[] array, int index, int value) {

 	int length = array.length;

 	if (index > length) {
 		throw new RuntimeException("Invalid index - " + index);
 	}

 	List<Integer> list = new ArrayList<Integer>(Arrays.asList(array));

 	list.add(index - 1, value);

 	Integer[] newArray = list.toArray(array);

 	return newArray;
 }


This code also produces the same output.


5. Conclusion


In this article, we've seen how to insert a new value into an array at the specific index in java in two ways.



No comments:

Post a Comment

Please do not add any spam links in the comments section.