-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyArrayList.java
More file actions
102 lines (86 loc) · 2.29 KB
/
MyArrayList.java
File metadata and controls
102 lines (86 loc) · 2.29 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
public class MyArrayList<E>
{
private int size; // Number of elements in the list
private E[] data;
private int MAXELEMENTS = 50;
/** Create an empty list */
public MyArrayList() {
data = (E[])new Object[MAXELEMENTS];// cannot create array of generics
size = 0; // Number of elements in the list
}
public int getMAXELEMENTS(){
return MAXELEMENTS;
}
public boolean checkSpace()
{
if (size<MAXELEMENTS)
return true;
else
return false;
}
public void add(int index, E e) {
// Ensure the index is in the right range
if (index < 0 || index > size)
throw new IndexOutOfBoundsException
("Index: " + index + ", Size: " + size);
// Move the elements to the right after the specified index
for (int i = size - 1; i >= index; i--)
data[i + 1] = data[i];
// Insert new element to data[index]
data[index] = e;
// Increase size by 1
size++;
}
public boolean contains(Object e) {
for (int i = 0; i < size; i++)
if (e.equals(data[i])) return true;
return false;
}
public E get(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException
("Index: " + index + ", Size: " + size);
return data[index];
}
public E remove(int index) {
if (index < 0 || index >= size)
throw new IndexOutOfBoundsException
("Index: " + index + ", Size: " + size);
E e = data[index];
// Shift data to the left
for (int j = index; j < size - 1; j++)
data[j] = data[j + 1];
data[size - 1] = null; // This element is now null
// Decrement size
size--;
return e;
}
public void clear()
{
size = 0;
}
public String toString() {
String result="[";
for (int i = 0; i < size; i++) {
result+= data[i];
if (i < size - 1) result+=", ";
}
return result.toString() + "]";
}
public boolean checkUniform() {
if (size == 0)
return true;
else
{
// compare everyone with first element
for (int i = 1; i < size; i++) {
if(((Comparable)data[0]).compareTo(data[i])!=0)
return false;
}
return true;
}
}
public int getSize() {
return size;
}
}