0% found this document useful (0 votes)
5 views7 pages

Array Operation

arrat

Uploaded by

vani shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views7 pages

Array Operation

arrat

Uploaded by

vani shree
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Inserting an element into an array:

// Java Program to add an element in an Array

import java.io.*;

import java.lang.*;

import java.util.*;

class GFG {

// Function to add x in arr

public static int[] addX(int n, int arr[], int x)

int i;

// create a new array of size n+1

int newarr[] = new int[n + 1];

// insert the elements from

// the old array into the new array


// insert all elements till n

// then insert x at n+1

for (i = 0; i < n; i++)

newarr[i] = arr[i];

newarr[n] = x;

return newarr;

// Driver code

public static void main(String[] args)

int n = 10;

int i;

// initial array of size 10

int arr[]
= { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };

// print the original array

System.out.println("Initial Array:\n"

+ Arrays.toString(arr));

// element to be added

int x = 50;

// call the method to add x in arr

arr = addX(n, arr, x);

// print the updated array

System.out.println("\nArray with " + x

+ " added:\n"

+ Arrays.toString(arr));

Output:
Initial Array:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Array with 50 added:
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 50]

Deleting an element in a particular position:

import java.util.Arrays;

class GFG {

// Function to remove the element

public static int[] removeTheElement(int[] arr, int index)

// If the array is empty

// or the index is not in array range

// return the original array

if (arr == null || index < 0

|| index >= arr.length) {

return arr;
}

// Create another array of size one less

int[] anotherArray = new int[arr.length - 1];

// Copy the elements except the index

// from original array to the other array

for (int i = 0, k = 0; i < arr.length; i++) {

// if the index is

// the removal element index

if (i == index) {

continue;

// if the index is not

// the removal element index

anotherArray[k++] = arr[i];

}
// return the resultant array

return anotherArray;

// Driver Code

public static void main(String[] args)

// Get the array

int[] arr = { 1, 2, 3, 4, 5 };

// Print the resultant array

System.out.println("Original Array: "

+ Arrays.toString(arr));

// Get the specific index

int index = 2;
// Print the index

System.out.println("Index to be removed: " + index);

// Remove the element

arr = removeTheElement(arr, index);

// Print the resultant array

System.out.println("Resultant Array: "

+ Arrays.toString(arr));

Output
Original Array: [1, 2, 3, 4, 5]
Index to be removed: 2
Resultant Array: [1, 2, 4, 5]

You might also like