Lists - How To Build Your Own: Atul Prakash Reference: Look at Arraylist - Java Implementation in Java
Lists - How To Build Your Own: Atul Prakash Reference: Look at Arraylist - Java Implementation in Java
Class MyList
Implement a list using arrays Methods:
remove element:
Constructors: default capacity and given capacity add(Object element) Object get(int index)
Some tests
import junit.framework.*; public class MyListTest extends TestCase { public void testCount() { MyList l = new MyList(); assertEquals(0, l.size()); l.add("abc"); l.add("def"); l.add(3); assertEquals(3, l.size()); } } public void testInserts() { MyList l = new MyList(); l.add("abc"); l.add("def"); l.add(3); assertEquals(3, l.get(2)); assertEquals(3, l.size()); // String s = l.findElement(0); // cast needed. // assertEquals("abc", s); }
Invariants: num = # of elements in the list. 0 <= num <= capacity. data array: rst num slots lled, followed by nulls
Constructors
public class MyList { // list of integers Object[] data; // list itself. null values at the end int capacity; // maximum capacity of the list int num; // current size of the list static final int DEFAULT_CAPACITY = 100; public MyList() {
this(DEFAULT_CAPACITY);
// call MyList(capacity).
public MyList(int capacity) { this.capacity = capacity; data = new Object[capacity]; // null array num = 0; }
Note that array is lled with null values, size is 0. Invariants hold.
adding elements
public class MyList { // list of integers Object[] data; // list itself. null values at the end int capacity; // maximum capacity of the list int num; // current size of the list static final int DEFAULT_CAPACITY = 100; public void add(Object a) { // add an object a to the end of the list data[num] = a; num++; }
Check if num is 0, that this works correctly. Check also if num == capacity.
Boundary checks
public class MyList { // list of integers Object[] data; // list itself. null values at the end int capacity; // maximum capacity of the list int num; // current size of the list static final int DEFAULT_CAPACITY = 100; public void add(Object a) throws CapcityExceeded { if (num == capacity) { throw new CapacityExceeded("list capacity exceeded"); } data[num] = a; num++; }
Retrieving an Element
public Object get(int index) { // find the element at given index if (index < 0 || index >= num) { throw RuntimeException("index out of bounds"); } return data[index]; }
Shift elements to the left. Can be extended to delete an element in the middle
Incorrect shifting
public void deleteFirstElement() { // delete first element from the list for (int i = num-1; i > 0; i--) { data[i-1] = data[i]; } num--; }
Could use additional functionality: updating an element searching for an element inserting an element at a given index
But arrays cannot change in size How do you increase the size of the
array?
Modied add
public void add(Object a) { if (num == capacity) { Object[] datanew = new Object[1+num]; // copy old data to new data for (int i = 0; i < num; i++) { datanew[i] = data[i]; } data = datanew; // data now refers to he new array capacity = capacity + 1; } // done extending the array if necessary. // now add the element data[num] = a; num++; }
Some Inefciencies
Every time we exceed the capacity, the array is copied. For example, if capacity is 10,000, and we add 3 elements:
Capacity-doubling Strategy
public void add(Object a) { if (num == capacity) {
// copy old data to new data for (int i = 0; i < num; i++) { datanew[i] = data[i]; } data = datanew; // data now refers to he new array } data[num] = a; num++;
More Efcient
If the capacity is 10,000, adding 3 elements
on a full list only causes the array to be copied once. additions.
Next copy will occur after 10,000 Overall, copying becomes less frequent.
Variations of Lists
Stacks:
Don't allow duplicate objects in the list (modify add to check for duplicates).
Queues: