-
Notifications
You must be signed in to change notification settings - Fork 1
/
MyArray.java
207 lines (199 loc) · 5.71 KB
/
MyArray.java
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.
*/
package sortedinsertionpractice;
import java.util.InputMismatchException;
import java.util.Scanner;
/**
*
* @author eapple
*/
public class MyArray
{
int fillindex = -1;
Scanner input = new Scanner(System. in);
int size;
int list[];
public MyArray(int s)
{
size = s;
}
public void FillSortedArray()
{
list = new int [size];
for(int i=0; i<list.length; i++)
{
int baag = 0;
System.out.println("enter "+i+" element of your array");
int num = input.nextInt();
list[i] = num;
fillindex++;
try
{
if(fillindex == -1)
{
System.out.println("The array is empty filhal ");
}
else if(fillindex == list.length - 1)
{
System.out.println("you can not insert, because the array is full fihal");
}
else
{
for(int j =0; j<=list.length; j++)
{
for(int k=j+1; k<=list.length; k++)
{
if(list[j] > list[k])
{
// phyton logic list[j] + list[k] == list[k] + list[j];
}
}
}
}
}
catch (InputMismatchException e)
{
input.next();
System.out.println("Invalid input in sorting method");
}
}
}
public void FindElement()
{
try
{
int flag=0;
System.out.println("which element you want to find");
int find = input.nextInt();
for(int i =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 (InputMismatchException e)
{
input.next();
System.out.println("Input is invalid.\n" +"Please enter digits only");
}
}
public void DeleteElement()
{
try
{
System.out.println("which element you want to delete");
int element = input.nextInt();
int deleteindex = -1;
for(int i = 0; i<list.length; i++)
{
if(element == list[i])
{
list[i] = 0;
deleteindex = i;
fillindex --;
break;
}
}
if (deleteindex != -1)
{
int key = 0;
for(int i = 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 (InputMismatchException e)
{
input.next();
System.out.println("invalid inputtttttttt ");
}
}
void Modify()
{
try
{
System.out.println("at what index you want to make change");
int index = input.nextInt();
System.out.println("what do you want to insert at " + index);
int modify = input.nextInt();
for(int i=0; i<list.length; i++)
{
if (list[index]== i)
{
list[index]= modify;
break;
}
}
}
catch(Exception e)
{
System.out.println("Array na bahar shwi rora !!! \n sam shaaaa !! ");
}
}
public void display()
{
System.out.println("array elements are: ");
for(int i=0;i<list.length;i++)
{
System.out.print(list[i]+" ");
}
}
public void insert()
{
try
{
System.out.println("what do you want to insert " );
int insert = input.nextInt();
if(fillindex == -1)
{
System.out.println("The array is empty ");
}
else if(fillindex == list.length-1)
{
System.out.println("you can not insert, because the array is full ");
}
else if(fillindex < list.length)
{
fillindex ++;
list[fillindex] = insert;
}
}
catch(Exception e)
{
{
System.out.println("wran input !!!!!!!!!!!!!!!! ");
}
}
}
}