Home Tutorial Java Core Insert and Delete an element in between an array

 
 

Share on Google+Share on Google+
Insert and Delete an element in between an array
Posted on: January 15, 2010 at 12:00 AM
Advertisement
In this section, you will learn how to insert and delete an element in between an array in Java.

Insert and Delete an element in between an array

In this section, you will learn how to insert and delete an element in between an array. For this purpose, we have created two separate methods for inserting and deleting an element with three parameters, first is the index,other is the entire array and the last is the value to be inserted or deleted.These parameter make the methods to return a new array.

Following code insert an element in an array:

public int[] insertArray(int index, int[] arr, int value) {
		int[] newArray = new int[arr.length + 1];
		System.arraycopy(arr, 0, newArray, 0, index);
		System.arraycopy(arr, index, newArray, index + 1, 
                         arr.length - index);
		newArray[index] = value;
		arr = newArray;
		return newArray;
	}

Following code remove an element from an array:

public int[] deleteFromArray(int index, int[] arr, int value) {
		boolean[] deleteNumber = new boolean[arr.length];
		int size = 0;
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] == value) {
				deleteNumber[i] = true;
			} else {
				deleteNumber[i] = false;
				size++;
			}
		}
		int[] newArr = new int[size];
		int in = 0;
		for (int i = 0; i < arr.length; i++) {
			if (!deleteNumber[i]) {
				newArr[in++] = arr[i];
			}
		}
		return newArr;
	}

Here is the code:

class InsertNumber {
	public int[] insertArray(int index, int[] arr, int value) {
		int[] newArray = new int[arr.length + 1];
		System.arraycopy(arr, 0, newArray, 0, index);
		System.arraycopy(arr, index, newArray, index + 1, 
                   arr.length - index);
		newArray[index] = value;
		arr = newArray;
		return newArray;
	}

	public int[] deleteFromArray(int index, int[] arr, int value) {
		boolean[] deleteNumber = new boolean[arr.length];
		int size = 0;
		for (int i = 0; i < arr.length; i++) {
			if (arr[i] == value) {
				deleteNumber[i] = true;
			} else {
				deleteNumber[i] = false;
				size++;
			}
		}
		int[] newArr = new int[size];
		int in = 0;
		for (int i = 0; i < arr.length; i++) {
			if (!deleteNumber[i]) {
				newArr[in++] = arr[i];
			}
		}
		return newArr;
	}

	public static void main(String[] args) {
		int arr[] = { 1, 2, 3, 4, 5 };
		InsertNumber in = new InsertNumber();
		System.out.println("After adding an element to Array 
                  arr[], array becomes:");
		int num[] = in.insertArray(1, arr, 8);
		for (int i = 0; i < num.length; i++) {
			System.out.println(num[i]);
		}
		System.out.println("After removing an element from  
                  Array arr[], array becomes:");
		int newArr[] = in.deleteFromArray(1, arr, 2);
		for (int i = 0; i < newArr.length; i++) {
			System.out.println(newArr[i]);
		}
	}
}

Output:

After adding an element to Array arr[], array becomes:
1
8
2
3
4
5
After removing an element from Array arr[], array becomes:
1
3
4
5
Advertisement

Related Tags for Insert and Delete an element in between an array:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: January 15, 2010

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com