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

Code after lecture04

parent 10098307
No related merge requests found
Showing with 121 additions and 0 deletions
+121 -0
import java.util.ArrayList;
import java.util.TreeSet;
public class ArrayListClient {
// Set<String> set = new TreeSet<>();
public static void main(String[] args) {
IList<String> list = new OurArrayList<>();
for (int i = 0; i < 19; i++) {
list.add("" + i + "!");
}
System.out.println(list);
/*
for (String s : list) {
}
*/
/*
int[] =======> [int][int][int].....
Object[] ====> [ | ][ | ][ | ]
v v v
*/
}
}
\ No newline at end of file
public interface IList<E> {
public int size();
public void set(int idx, E elem);
public E get(int idx);
public void clear();
public void add(int idx, E elem);
public void add(E elem);
}
public class OurArrayList<E> implements IList<E> {
private E[] data;
private int size;
// size vs. capacity
// current size = 0
// max size (capacity) = 10
public OurArrayList() {
this.data = (E[])new Object[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 + "]";
}
/**
* 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) {
}
@Override
public int size() {
return this.size;
}
@Override
public void set(int idx, E elem) {
if (idx < 0 || idx > this.size) {
throw new IllegalArgumentException("blah");
}
else if (idx == size) {
add(elem);
}
this.data[idx] = elem;
}
public E get(int idx) {
return null;
}
@Override
public void clear() {
this.size = 0;
}
public void add(E elt) {
if (this.size == this.data.length) {
E[] newData = (E[])new Object[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++;
}
@Override
public void add(int idx, E elem) {
}
}
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