-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperArray.java
More file actions
154 lines (136 loc) · 3.44 KB
/
SuperArray.java
File metadata and controls
154 lines (136 loc) · 3.44 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
public class SuperArray {
private String [] data;
private int size; //The current size
public SuperArray() {
data = new String[10];
size = 0;
}
public SuperArray(int initialCapacity) {
if (initialCapacity < 0) {
throw new IllegalArgumentException("Initial Capaity cannot be a negative value.");
}
if (initialCapacity >= 0) {
data = new String[initialCapacity];
size = 0;
}
}
public int size() {
return size;
}
public boolean add(String element) {
if (size + 1 >= data.length) {
resize();
}
data[size] = element;
size ++;
return true;
}
public String get(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index less than 0 or Greater than/Equal to the size of the array");
}
return data[index];
}
public String set(int index, String element) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index less than 0 or Greater than/Equal to the size of the array");
}
String returnString = data[index];
data[index] = element;
return returnString;
}
public void resize() {
String [] tempArray = new String[(size * 2) + 1];
for(int i = 0; i < data.length; i++) {
tempArray[i] = data[i];
}
data = tempArray;
}
public boolean isEmpty() {
return size==0;
}
public void clear() {
data = new String[size];
size = 0;
}
public String toString() {
String CurrentArray = "";
for(int current = 0; current < size(); current++){
if (current + 1 < size()) {
CurrentArray += get(current) + ", ";
} else {
CurrentArray += get(current);
}
}
return "[" + CurrentArray +"]";
}
public void add(int index, String element) {
if (index < 0 || index > size()) {
throw new IndexOutOfBoundsException("Index less than 0 or Greater than the size of the array");
}
for(int i = size - 1; i >= index; i-- ) {
if (size + 1 >= data.length) {
resize();
}
data[i + 1] = data[i];
}
data[index] = element;
size ++;
}
public String remove(int index) {
if (index < 0 || index >= size()) {
throw new IndexOutOfBoundsException("Index less than 0 or Greater than/Equal to the size of the array");
}
String returnValue = data[index];
for (int i = index; i < size() - 1; i++ ) {
data[i] = data[i + 1];
}
data[size() - 1] = null;
size--;
return returnValue;
}
public int indexOf (String s) {
for (int i = 0; i < size(); i++) {
if (data[i].equals(s)) {
return i;
}
}
return -1;
}
public String[] toArray() {
String [] rArray = new String[size];
for (int i = 0; i < size; i++) {
rArray[i] = data[i];
}
return rArray;
}
public boolean contains(String s) {
boolean contained = false;
for(int i = 0; i < size; i++) {
if ((data[i].equals(s)) && (data[i] != null)) {
contained = true;
}
}
return contained;
}
public int lastIndexOf(String value){
int x = -1;
for (int i = 0; i < size(); i++) {
if (data[i].equals(value)) {
x = i;
}
}
return x;
}
public boolean equals(SuperArray other){
if (size() != other.size()) {
return false;
}
for (int i = 0; i < size() && i < other.size(); i++) {
if (!data[i].equals(other.get(i))) {
return false;
}
}
return true;
}
}