-
Notifications
You must be signed in to change notification settings - Fork 0
/
arrayClass.java
65 lines (51 loc) · 1.34 KB
/
arrayClass.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
public class arrayClass {
private int[] items;
private int count = 0;
public arrayClass(int length) {
items = new int[length];
}
public void insert(int item) {
if (items.length == count) {
int[] newItems = new int[count * 2];
for (int i = 0; i < count; i++) {
newItems[i] = items[i];
}
items = newItems;
}
items[count++] = item;
}
public int indexOf(int item) {
for(int i = 0; i < count; i++)
{
if (items[i] == item)
return i;
}
return -1;
}
public void removeAt(int index) {
if(index < 0 || index >=count)
{
throw new IllegalArgumentException();
}
for(int i = index; i < count; i++)
{
items[i] = items[i+1];
count--;
}
}
public void print() {
for (int i = 0; i < count; i++)
System.out.println(items[i]);
}
public static void main(String[] args) {
arrayClass array = new arrayClass(3);
array.insert(10); //0
array.insert(20); //1
array.insert(30); //2
array.insert(40); //3
array.insert(50); //4
array.removeAt(4);
System.out.println(array.indexOf(50));
array.print();
}
}