1// Java program to delete specified integer from an array
2import java.util.Scanner;
3public class DeleteSpecifiedInteger
4{
5 public static void main(String[] args)
6 {
7 int num, n, temp = 1, place = 0;
8 Scanner sc = new Scanner(System.in);
9 System.out.println("Please enter number of elements: ");
10 num = sc.nextInt();
11 int[] arrNum = new int[num];
12 System.out.println("Please enter all the elements: ");
13 for(int a = 0; a < num; a++)
14 {
15 arrNum[a] = sc.nextInt();
16 }
17 System.out.println("Enter the element you want to delete: ");
18 n = sc.nextInt();
19 for(int a = 0; a < num; a++)
20 {
21 if(arrNum[a] == n)
22 {
23 temp = 1;
24 place = a;
25 break;
26 }
27 else
28 {
29 temp = 0;
30 }
31 }
32 if(temp == 1)
33 {
34 for(int a = place + 1; a < num; a++)
35 {
36 arrNum[a - 1] = arrNum[a];
37 }
38 System.out.println("After deleting element: ");
39 for(int a = 0; a < num - 2; a++)
40 {
41 System.out.print(arrNum[a] + ",");
42 }
43 System.out.print(arrNum[num - 2]);
44 }
45 else
46 {
47 System.out.println("Element not found!!");
48 }
49 sc.close();
50 }
51}
1// remove element from a specific index from an array
2import java.util.Arrays;
3public class DeleteElementDemo
4{
5 // remove element method
6 public static int[] removeElement(int[] arrGiven, int index)
7 {
8 // if empty
9 if(arrGiven == null || index < 0 || index >= arrGiven.length)
10 {
11 return arrGiven;
12 }
13 // creating another array one less than initial array
14 int[] newArray = new int[arrGiven.length - 1];
15 // copying elements except index
16 for(int a = 0, b = 0; a < arrGiven.length; a++)
17 {
18 if(a == index)
19 {
20 continue;
21 }
22 newArray[b++] = arrGiven[a];
23 }
24 return newArray;
25 }
26 public static void main(String[] args)
27 {
28 int[] arrInput = { 2, 4, 6, 8, 10 };
29 // printing given array
30 System.out.println("Given array: " + Arrays.toString(arrInput));
31 // getting specified index
32 int index = 3;
33 // print index
34 System.out.println("Index to be removed: " + index);
35 // removing element
36 arrInput = removeElement(arrInput, index);
37 // printing new array
38 System.out.println("New array: " + Arrays.toString(arrInput));
39 }
40}