DAY 1 : List ADT and ArrayList #100DaysOfCode
2 min readJan 25, 2018
Today’s Learning: ArrayList (Resizeable list using static arrays), Abstract Data Type, Interfaces, Generics
What Do i want to Do?
- I want to store my data items in a linear order for easy access without the constraint of specifying what type and quantity of data items.
What are the solutions ?
- You can make an array and store your data objects in it, when your array gets full, create a new array of double its length, copy all items from original array to new array and delete old array. (ArrayList)
- You can make a singly or doubly linked list and store your data objects in the links, then create the new links at runtime. (LinkedList)
So how are we going to implement the solutions?
- Our goal is separating Client code, Interface and Implementation from each other so that we can change the underlying implementations without disturbing client code.
- First we will define the Abstract Data Type (LIST) in the form of java interface, it will list all the functionality which is required from our list. Any implementation implementing this interface will guarantee to apply those functionalities.
- Then we will implement ArrayList which will use a statically allocated array to store our objects, helper methods and methods specified in list interface will modify this array to achieve the required functionality.
- Then we will write a test class which will create a new ArrayList and will test all the methods.
LIST INTERFACE
- Add an element to the end of list.
- Add an element at specific index of list.
- Replace an element at specific index with given element.
- Query the number of elements in list.
- Query whether list is empty.
- Remove and return the element from the specified index, if index is valid.
- Find the first occurrence of a specified element and return its index.
ARRAYLIST IMPLEMENTATION
CLIENT CODE (TESTING CODE)
Then i tested my ArrayList by writing client code that can uses my List interface and make a new object which uses ArrayList as its underlying implementation.
Output of Testing Code
Final Words
I could not attend the meetup planned earlier, but i completed the design , implementation and testing of ArrayList. I am learning and improving so mistakes are natural, please correct me anywhere necessary. Will meet you tomorrow :)