-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSuperArray.java
More file actions
151 lines (134 loc) · 3.33 KB
/
Copy pathSuperArray.java
File metadata and controls
151 lines (134 loc) · 3.33 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
class SuperArray{
private String [] data;
private int size; //The current size
public SuperArray(){
data = new String[10];
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 " + index + " out of bounds.");
}
return data[index];
}
public String set(int index, String element){
if (index < 0 || index >= size()){
throw new IndexOutOfBoundsException("Index " + index + " out of bounds.");
}
String replaced = data[index];
data[index] = element;
return replaced;
}
private void resize(){
String[] old = new String[data.length];
for (int i = 0; i<data.length; i++){
old[i] = data[i];
}
data = new String[old.length*2];
for (int i = 0; i<old.length; i++){
data[i] = old[i];
}
}
public void clear(){
data = new String[10];
size = 0;
}
public boolean isEmpty(){
return size == 0;
}
public String toString(){
String result = "[";
for (int i = 0; i < size; i++){
result += data[i];
if (i < size - 1){
result += ", ";
}
}
return result+"]";
}
public boolean contains(String s){
for (int i = 0; i<size; i++){
if(data[i].equals(s)){
return true;
}
}
return false;
}
public SuperArray(int InitialCapacity){
if (InitialCapacity < 0){
throw new IllegalArgumentException("The input entered " + InitialCapacity + " is negative. It must be at least 0.");
}
data = new String[InitialCapacity];
size = 0;
}
public void add(int index, String element){
if (index < 0 || index > size()){
throw new IndexOutOfBoundsException("Index " + index + " out of bounds.");
}
String replaceWith = element;
String temp;
if (size+1 >= data.length){
resize();
}
for (int i = index; i<size+1; i++){
temp = data[i];
data[i] = replaceWith;
replaceWith = temp;
}
size++;
}
public String remove(int index){
if (index < 0 || index >= size()){
throw new IndexOutOfBoundsException("Index " + index + " out of bounds.");
}
String removed = data[index];
for (int i = index; i < size-1; i++){
data[i] = data[i+1];
}
data[size] = null;
size--;
return removed;
}
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[] result = new String[size];
for (int i = 0; i < size; i++){
result[i] = data[i];
}
return result;
}
public int lastIndexOf(String value){
for (int i = size-1; i >= 0; i++){
if (data[i].equals(value)){
return i;
}
}
return -1;
}
public boolean equals(SuperArray other){
for (int i = 0; i < size; i++){
if (!other.get(i).equals(data[i])){
return false;
}
}
return true;
}
}