-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharr.java
More file actions
94 lines (73 loc) · 2.48 KB
/
arr.java
File metadata and controls
94 lines (73 loc) · 2.48 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import java.util.Scanner;
public class arr {
public static void main(String[]args){
Scanner sc=new Scanner(System.in);
System.out.println("enter size of array");
int size=sc.nextInt();
int[]arr=new int[size+1];
for(int i=0;i<size;i++){
arr[i]=sc.nextInt();
}
while(true){
System.out.println();
System.out.println("1: insert at pos");
System.out.println("2: delete at pos");
System.out.println("3: delete a val");
System.out.println("4: exit");
System.out.println("5: traversl");
System.out.println("enter choice: ");
int choice=sc.nextInt();
switch(choice){
case 1:
System.out.print("enter element to insert: ");
int element=sc.nextInt();
System.out.println("enter pos to insert: ");
int pos1=sc.nextInt();
for(int i=size;i>=pos1;i--){
arr[i]=arr[i-1];
}
size++;
arr[pos1-1]=element;
break;
case 2:
System.out.println("enter pos to delete: ");
int pos2=sc.nextInt();
for(int i=pos2-1;i<size-1;i++){
arr[i]=arr[i+1];
}
size--;
break;
case 3:
System.out.println("enter element you want to delete: ");
int elem=sc.nextInt();
int pos3=-1;
for(int i=0;i<size;i++){
if(arr[i]==elem){
pos3=i;
break;
}
}
if(pos3==-1){
System.out.println("not exists");
}else{for(int i=pos3;i<size-1;i++){
arr[i]=arr[i+1];
}
}
size--;
break;
case 4:
System.out.println("exiting..");
return;
case 5:
System.out.println("array: ");
for(int i=0;i<size;i++){
System.out.print(arr[i]+" ");
}
break;
default:
System.out.println("wrong choice ");
break;
}
}
}
}