Commit 10098307 authored by Adam Blank's avatar Adam Blank
Browse files

Code after lecture03

parent 55e80af8
Showing with 63 additions and 0 deletions
+63 -0
public class ArrayIntList {
private int[] data;
private int size;
// size vs. capacity
// current size = 0
// max size (capacity) = 10
public ArrayIntList() {
this.data = new int[10];
this.size = 0;
}
//[1, 2, 3]
public String toString() {
String elements = "";
for (int i = 0; i < this.size; i++) {
elements += this.data[i] + ", ";
}
if (elements.length() > 0) {
elements = elements.substring(0, elements.length() - 2);
}
return "[" + elements + "]";
}
public void add(int elt) {
if (this.size == this.data.length) {
int[] newData = new int[this.size * 2];
for (int i = 0; i < this.data.length; i++) {
newData[i] = this.data[i];
}
this.data = newData;
}
this.data[size] = elt;
this.size++;
}
/**
* Puts element at index idx, shifting over everything after to the right.
* @param idx
* @param element
* For example, idx = 2, element = 42
* [1, 2, 3, 4, 5]
* ^
* [1, 2, 42, 3, 4, 5]
*/
public void add(int idx, int element) {
}
public int get(int idx) {
return -1;
}
}
public class ArrayIntListClient {
public static void main(String[] args) {
ArrayIntList list = new ArrayIntList();
for (int i = 0; i < 100; i++) {
list.add(i);
}
System.out.println(list);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment