-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeleteElement
50 lines (31 loc) · 1.11 KB
/
DeleteElement
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package assignments_day_three
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class DeleteElement
{
public static void main (String[]args)
{
Scanner in = new Scanner(System.in);
//random generated array
int[] intArr = {13, 7, 6, 45, 21, 9, 2, 100};
//code to delete a specific element from an array
System.out.print("Enter Element to be deleted : ");
int elem = in.nextInt();
for(int i = 0; i < intArr.length; i++){
if(intArr[i] == elem)
{
//shifting elements
for(int j = i; j < intArr.length - 1; j++)
{
intArr[j] = intArr[j+1];
}
break;
}
}
System.out.println("Elements -- " );
for(int i = 0; i < intArr.length; i++){
System.out.print(" " + intArr[i]);
}
}
}