CSC1110 Exercise 8

Arrays vs ArrayLists

Arrays are the foundation for many collections in object oriented languages. Some collections, like the ArrayList, even use an array itself to store its data. But even with convenient classes like ArrayLists there are still many uses for arrays in modern programming. Arrays generally use very little memory compared to ArrayLists and can often be faster to retrieve data from. They also tend to use simpler syntax when you need multiple dimensions. Take, for example, a 2D array of integers vs a 2D ArrayList of integers:

int[][] arr = new int[5][5];
ArrayList<ArrayList<Integer>> list = new ArrayList();

Both represent a collection of collections. The first is an array of arrays of int variables. The second is a list of lists of Integer objects. To access the third element of the second collection would look like this:

int x = arr[1][2];
int y = list.get(1).get(2);

Remember, everything is zero-indexed, so the index values are 1 and 2, not 2 and 3. Also, the first dimension is which of the collections we are looking in, so we first have to access the second collection (index 1) and then access the value in that collection (index 2).

Useful ArrayList Methods

One of the chief benefits of using ArrayLists are the prebuilt methods that we can use. Arrays are not normal objects and do not have any methods. They have a single instance variable, length, that only tells you how many indexes are in the array. ArrayLists have methods that can do a variety of tasks ranging from simple to complex, and have additional information, such as the number of elements currently in the ArrayList. An ArrayList serves as a "wrapper class" to the array, in much the same way the Integer class does with an int, giving it methods and additional functionality.

You will write the following methods for an array, then do the same thing with an ArrayList to check your work. Use the ArrayList documentation to find the specifics of the methods.

Sample Output

ARR: [4, 8, 1, 3, 5, 6, 3, 6, 2, 6, 7]

ARR: 5
ARR: 9
Removed from arr: 1
ARR: [4, 8, 3, 5, 6, 3, 6, 2, 6, 7]
Removed from List: 1
LIST: [4, 8, 3, 5, 6, 3, 6, 2, 6, 7]
ARR: [9, 4, 8, 3, 5, 6, 3, 6, 2, 6, 7]
LIST: [9, 4, 8, 3, 5, 6, 3, 6, 2, 6, 7]

Submission Instructions

Commit and Push your files to GitHub Classroom.