Expand|Select|Wrap|Line Numbers
- import java.util.ArrayList;
- public class myArrayList {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args)
- {
- ArrayList myArrayList = new ArrayList();
- private ArrayList[] accounts;
- private int numAccounts;
- //This is my first constructor which has an empty list with an initial capacity of ten.
- public myArrayList()
- {
- accounts = new ArrayList[10];
- numAccounts = 0;
- }
- //This is my second Constructor which has an empty list with an initial cappacity of 'initialCapacity'.
- public myArrayList(int initialCapacity)
- {
- accounts = new ArrayList[initialCapacity];
- numAccounts = 0;
- }
- //This will Return the number of elements in the List.
- public int size()
- {
- return numAccounts;
- }
- //This Tests will show if the list has no elements, and returns true if the list is empty and false otherwise.
- public boolean isEmpty()
- {
- if(numAccounts == 0)
- return true;
- else
- return false;
- }
- /**
- * Returns true if this list contains the specified element.
- * @param b element whose presence in this List is to be tested.
- * @return true if the specified element is present; false otherwise.
- */
- public boolean contains(ArrayList b)
- {
- int index = find(b);
- if(index == -1)
- {
- return false;
- }
- else
- return true;
- }
- //This Removes all of the elements from this list. The list will thus be empty.
- public void clear()
- {
- for(int i = 0; i != numAccounts; i ++) {
- accounts[i] = null;
- }
- numAccounts = 0;
- }
- /**
- * Replaces the element at the specified position in this list with the specified element.
- * @param index index of element to replace.
- * @param b element to be stored at the specified position.
- * @return the element previously at the specified position.
- */
- public ArrayList set(int index, ArrayList b)
- {
- accounts[index] = b;
- return null;
- }
- /**
- * Inserts the specified element at the specified position in this list.
- * Shifts the element currently at that position (if any) and any
- * subsequent elements to the right (adds one to their indices).
- * @param b element to be inserted.
- * @return
- */
- public boolean add(ArrayList b)
- {
- //if the array is full
- if(numAccounts == accounts.length)
- {
- ArrayList [] newData = new ArrayList[accounts.length + 10];
- System.arraycopy(accounts, 0, newData, 0, accounts.length);
- accounts = newData;
- }
- accounts[numAccounts] = b;
- numAccounts ++;
- return true;
- }
- /**
- * Appends the specified element to the end of this list
- * inserts the element b at the index 'index'
- */
- public void add(int index, ArrayList b)
- {
- //if the array is full
- if(numAccounts == accounts.length)
- {
- ArrayList [] newData = new ArrayList[accounts.length + 10];
- System.arraycopy(accounts, 0, newData, 0, accounts.length);
- accounts = newData;
- }
- if(index != numAccounts)
- {
- System.arraycopy(accounts, index, accounts, index + 1, numAccounts - index);
- }
- accounts[index] = b;
- numAccounts ++;
- }
- /**
- * Removes the element at the specified position in this list. Shifts
- * any subsequent elements to the left (subtracts one from their indices).
- * @param index the index of the element to removed.
- * @return the element that was removed from the list.
- */
- public ArrayList remove(int index)
- {
- //if the index given is invalid
- if(0 < index || index >= numAccounts)
- {
- throw new RuntimeException("remove: invalid index");
- }
- System.arraycopy(accounts, index + 1, accounts, index, numAccounts - 1);
- numAccounts --;
- return null;
- }
- /**
- * Removes a single instance of the specified element from this list,
- * if it is present (optional operation). More formally, removes an
- * element e such that (o==null ? e==null : o.equals(e)), if the list
- * contains one or more such elements. Returns true if the list contained
- * the specified element (or equivalently, if the list changed as a result
- * of the call).
- * @param b element to be removed from this list, if present.
- * @return true if the list contained the specified element.
- */
- public boolean remove(ArrayList b)
- {
- int index = find(b);
- if(index == -1)
- {
- return false;
- }
- else
- {
- remove(index);
- numAccounts --;
- return true;
- }
- }
- /**
- * Returns the element at the specified position in this list.
- * @param index index of element to return.
- * @return the element at the specified position in this list.
- */
- public ArrayList get(int index)
- {
- if(0 < index || index >= accounts.length)
- {
- throw new RuntimeException("remove: invalid index");
- }
- else
- return accounts[index];
- }
- //returns index of ArrayList b, returns -1 if not found
- private int find(ArrayList b)
- {
- for(int i =0 ; i != numAccounts; i ++)
- {
- if(accounts[i] == b)
- return i;
- }
- return -1;
- }
- }
- }