-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyArrayList.java
More file actions
137 lines (120 loc) · 3.63 KB
/
MyArrayList.java
File metadata and controls
137 lines (120 loc) · 3.63 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
https://powcoder.com
代写代考加微信 powcoder
Assignment Project Exam Help
Add WeChat powcoder
/*
* I attest that the code in this file is entirely my own except for the starter
* code provided with the assignment and the following exceptions:
* <
* Enter all external resources and collaborations here. Note external code may
* reduce your score but appropriate citation is required to avoid academic
* integrity violations. Please see the Course Syllabus as well as the
* university code of academic integrity:
* https://catalog.upenn.edu/pennbook/code-of-academic-integrity/
* >
* Signed,
* Author: YOUR NAME HERE
* Penn email: <YOUR-EMAIL-HERE@seas.upenn.edu>
* Date: <YYYY-MM-DD>
*/
public class MyArrayList<E> {
/*
* Do not change this initial capacity; it is used by our test cases
*/
private static final int INITIAL_CAPACITY = 4;
/*
* These are protected so that test cases can access them. Please do not change
* the visibility of these fields!
*/
protected String[] data;
protected int size = 0;
public MyArrayList() {
data = new String[INITIAL_CAPACITY];
}
/*
* The requirements for this constructor are in
* Create Constructor Section in the assignment pdf
*/
public MyArrayList(String[] arr) {
}
public String get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
} else
return data[index];
}
private void increaseCapacity() {
String[] newData = new String[Math.max(2 * data.length, INITIAL_CAPACITY)];
System.arraycopy(data, 0, newData, 0, size);
data = newData;
}
/*
* This method adds the element to the list. Except for modifying it to use Java
* Generics, DO NOT OTHERWISE CHANGE THIS METHOD as it is used in testing your
* code.
*/
public void add(String value) {
if (size == data.length) {
increaseCapacity();
}
data[size++] = value;
}
public void add(int index, String element) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
if (size == data.length) {
increaseCapacity();
}
for (int i = size - 1; i >= index; i--) {
data[i + 1] = data[i];
}
data[index] = element;
size++;
}
public String remove(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException();
}
String target = data[index];
for (int i = index; i < size - 1; i++) {
data[i] = data[i + 1];
}
size--;
data[size] = null;
return target;
}
/*
* The requirements for this method are in
* remove method Section in the assignment pdf
*/
public boolean remove(String obj) {
return false;
}
public void print() {
for (int i = 0; i < size; i++) {
System.out.println(i + ": " + data[i]);
}
}
public boolean contains(String obj) {
for (int i = 0; i < size; i++) {
if (obj == data[i] || (data[i] != null && data[i].equals(obj)))
return true;
}
return false;
}
/*
* The requirements for this method are in
* set(index, value) method Section in the assignment pdf
*/
public String set(int index, String obj) {
return null;
}
/*
* The requirements for this method are in
* indexOf method Section in the assignment pdf
*/
public int indexOf(String obj) {
return -1;
}
}