-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathVector.java
107 lines (93 loc) · 2.46 KB
/
Vector.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
public class Vector {
private Object[] objects;
private int size;
private int capacity;
public Vector() {
capacity = 100;
size = 0;
objects = new Object[capacity];
}
public void push(Object object) {
if(size == capacity){
objects = resize(capacity * 2);
}
objects[size] = object;
size++;
}
public void pop() {
if (!isEmpty()) {
size--;
objects[size] = null;
} else {
System.err.println("Vector is empty!");
}
}
public Object at(int index) {
index--;
if(index < size) return objects[index];
else {
System.err.println("Index is out of size!");
return null;
}
}
public int find(Object item) {
for (int i = 0; i < size; i++) {
if (objects[i] == item) return i+1;
}
System.err.println("Not Found!");
return -1;
}
public void delete(int index) {
index--;
if (index >= size) {
System.err.println("Index is out of bound!");
return;
}
System.arraycopy(objects, index + 1, objects, index, size - 1 - index);
size--;
objects[size] = null;
}
public void insert(int index, Object item) {
index--;
if (index >= size) {
System.err.println("Index is out of bound!");
return;
}
for (int i = size; i > index; i--) {
objects[i] = objects[i-1];
}
objects[index] = item;
size++;
}
public void prepend(Object item) {
insert(1, item);
}
public void remove(Object item) {
if(find(item) != -1) delete(find(item));
}
public int getSize() {
return size;
}
public int getCapacity() {
return capacity;
}
public boolean isEmpty() {
return size == 0;
}
private Object[] resize(int capacity){
this.capacity = capacity;
Object[] objects1 = new Object[capacity];
System.arraycopy(objects, 0, objects1, 0, size);
return objects1;
}
@Override
public String toString() {
StringBuilder str = new StringBuilder("Vector:");
if (size == 0) return "Empty Vector!";
for (int i = 0; i < size; i++) {
str.append(" ").append(objects[i]);
}
str.append("\nSize: ").append(size);
return str.toString();
}
}