forked from aneez9n/Basic-Projects
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyArray.java
More file actions
Latest commit
207 lines (199 loc) · 5.71 KB
/
Copy pathMyArray.java
File metadata and controls
207 lines (199 loc) · 5.71 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
packagesortedinsertionpractice;
importjava.util.InputMismatchException;
importjava.util.Scanner;
/**
*
* @author eapple
*/
publicclassMyArray
{
intfillindex = -1;
Scannerinput = newScanner(System. in);
intsize;
intlist[];
publicMyArray(ints)
{
size = s;
}
publicvoidFillSortedArray()
{
list = newint [size];
for(inti=0; i<list.length; i++)
{
intbaag = 0;
System.out.println("enter "+i+" element of your array");
intnum = input.nextInt();
list[i] = num;
fillindex++;
try
{
if(fillindex == -1)
{
System.out.println("The array is empty filhal ");
}
elseif(fillindex == list.length - 1)
{
System.out.println("you can not insert, because the array is full fihal");
}
else
{
for(intj =0; j<=list.length; j++)
{
for(intk=j+1; k<=list.length; k++)
{
if(list[j] > list[k])
{
// phyton logic list[j] + list[k] == list[k] + list[j];
}
}
}
}
}
catch (InputMismatchExceptione)
{
input.next();
System.out.println("Invalid input in sorting method");
}
}
}
publicvoidFindElement()
{
try
{
intflag=0;
System.out.println("which element you want to find");
intfind = input.nextInt();
for(inti =0; i<list.length; i++)
{
if(list[i]==find)
{
System.out.println(find +" is found at index "+i);
flag = 1;
}
}
if(flag ==0)
{
System.out.println(find +" is not found in the array");
}
}
catch (InputMismatchExceptione)
{
input.next();
System.out.println("Input is invalid.\n" +"Please enter digits only");
}
}
publicvoidDeleteElement()
{
try
{
System.out.println("which element you want to delete");
intelement = input.nextInt();
intdeleteindex = -1;
for(inti = 0; i<list.length; i++)
{
if(element == list[i])
{
list[i] = 0;
deleteindex = i;
fillindex --;
break;
}
}
if (deleteindex != -1)
{
intkey = 0;
for(inti = deleteindex; i<list.length; i++)
{
if(i == deleteindex)
{
key = list[i+1];
list[i]= key;
}
else//below here indent
{
if(i==list.length-1)
{
list[i] = -1;
}
else
{
key = list[i+1];
list[i] = key;
}
}
}
}
else
{
System.out.println("could not find the element " + element);
}
}
catch (InputMismatchExceptione)
{
input.next();
System.out.println("invalid inputtttttttt ");
}
}
voidModify()
{
try
{
System.out.println("at what index you want to make change");
intindex = input.nextInt();
System.out.println("what do you want to insert at " + index);
intmodify = input.nextInt();
for(inti=0; i<list.length; i++)
{
if (list[index]== i)
{
list[index]= modify;
break;
}
}
}
catch(Exceptione)
{
System.out.println("Array na bahar shwi rora !!! \n sam shaaaa !! ");
}
}
publicvoiddisplay()
{
System.out.println("array elements are: ");
for(inti=0;i<list.length;i++)
{
System.out.print(list[i]+" ");
}
}
publicvoidinsert()
{
try
{
System.out.println("what do you want to insert " );
intinsert = input.nextInt();
if(fillindex == -1)
{
System.out.println("The array is empty ");
}
elseif(fillindex == list.length-1)
{
System.out.println("you can not insert, because the array is full ");
}
elseif(fillindex < list.length)
{
fillindex ++;
list[fillindex] = insert;
}
}
catch(Exceptione)
{
{
System.out.println("wran input !!!!!!!!!!!!!!!! ");
}
}
}
}