-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArray.java
More file actions
142 lines (112 loc) · 4.93 KB
/
Array.java
File metadata and controls
142 lines (112 loc) · 4.93 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
public class Array {
/** A simplified version of the java.util.List interface. */
public interface List<E> {
/** Returns the number of elements in this list. */
int size();
/** Returns whether the list is empty. */
boolean isEmpty();
/** Returns (but does not remove) the element at index i. */
E get(int i) throws IndexOutOfBoundsException;
/** Replaces the element at index i with e, and returns the replaced element. */
E set(int i, E e) throws IndexOutOfBoundsException;
/** Inserts element e to be at index i, shifting all subsequent elements later. */
void add(int i, E e) throws IndexOutOfBoundsException;
/** Removes/returns the element at index i, shifting subsequent elements earlier. */
E remove(int i) throws IndexOutOfBoundsException;
}
public static class ArrayList<E> implements List<E> {
// instance variables
public static final int CAPACITY=3; // default array capacity
private E[] data; // generic array used for storage
private int size = 0; // current number of elements
// constructors
public ArrayList() { this(CAPACITY); } // constructs list with default capacity
public ArrayList(int capacity) { // constructs list with given capacity
data = (E[]) new Object[capacity]; // safe cast; compiler may give warning
}
// protected utility method
/** Checks whether the given index is in the range [0, n-1]. */
protected void checkIndex(int i, int n) throws IndexOutOfBoundsException {
if (i < 0 || i >= n)
throw new IndexOutOfBoundsException("Illegal index: " + i);
}
// public methods
/** Returns the number of elements in the array list. */
public int size() { return size; }
/** Returns whether the array list is empty. */
public boolean isEmpty() { return size == 0; }
/** Returns (but does not remove) the element at index i. */
public E get(int i) throws IndexOutOfBoundsException {
checkIndex(i, size);
return data[i];
}
/** Replaces the element at index i with e, and returns the replaced element. */
public E set(int i, E e) throws IndexOutOfBoundsException {
checkIndex(i, size);
E temp = data[i];
data[i] = e;
return temp;
}
/** Inserts element e to be at index i, shifting all subsequent elements later. */
public void add(int i, E e) throws IndexOutOfBoundsException,IllegalStateException {
checkIndex(i, size + 1);
if (size == data.length) {
//---CHANGES MADE HERE---
E[] temp = (E[]) new Object[2 * data.length]; // safe cast; compiler may give warning
for (int k=0; k < size+1; k++) {
if (k == i) {
temp[k] = e;
}
else if (k > i) {
temp[k] = data[k-1];
}
else {
temp[k] = data[k];
}
}
data = temp;
size *= 2;
//---CHANGES MADE HERE---
}
//runs for normal add operations
else {
for (int k=size-1; k >= i; k--) { // start by shifting rightmost
data[k+1] = data[k];
}
data[i] = e; // ready to place the new element
size++;
}
}
/** Removes/returns the element at index i, shifting subsequent elements earlier. */
public E remove(int i) throws IndexOutOfBoundsException {
checkIndex(i, size);
E temp = data[i];
for (int k=i; k < size-1; k++) // shift elements to fill hole
data[k] = data[k+1];
data[size-1] = null; // help garbage collection
size--;
return temp;
}
public void printArray() {
for (int i = 0; i < size; i ++) {
System.out.print("["+get(i)+"]");
}
System.out.println("\n");
}
}
public static void main(String[] args) {
//initialization
ArrayList c;
c = new ArrayList<>();
//using array size limit of 3 to make it easier to see how method works
for(int i = 0; i<3; i++) {
c.add(i, i+1);
}
//list is already at 3, so adding another element will cause it to resize
c.add(1, 4);
//when this is run, it shoudl display [1][4][2][3] null null to indicate that the list:
//-resized to 6
//-added the next element
c.printArray();
}
}