Faculty of Science CSC 320L Lab2
Al Maaref University
Faculty of Sciences
Department of Computer Science
CSC 320 – Data Structures: Lab 2
Guidelines:
Submit only the java files as separate files (i.e. not as zipped files).
Refer to the Java files attached on Google classroom
Question: ArrayList Implementation
Consider the MUArrayList<E> implemented in Lesson 2. Implement the
following methods in the MUArraylist class:
1. public int indexOf (E item)
Find the first position of the specified item in the array list.
Example:
Input: indexOf("apple") on list [banana, apple, cherry, apple]
Output: 1
Explanation: The method searches from the start and returns the index 1, where "apple" first
appears.
2. public int LastIndexOf (E item)
Find the last position of the specified item in the array list.
Example:
Input: lastIndexOf("apple") on list [banana, apple, cherry, apple]
Output: 3
Explanation: The method searches from the end, returning the last index 3 where "apple"
appears.
Page 1 of 3
Faculty of Science CSC 320L Lab2
3. public int NumberOfOccurences (E item)
Count how many times the specified item appears in the array list.
Example:
Input: numberOfOccurrences("apple") on list [banana, apple, cherry, apple]
Output: 2
Explanation: This method counts occurrences of "apple" and returns 2, the number of times it
appears.
4. public void addOrRemove (E item)
Add the item if it’s not in the array list; remove it if it is already in the array list.
Example:
Initial List: [banana, cherry]
Input: addOrRemove("apple")
Output: [banana, cherry, apple]
Explanation: Since "apple" wasn’t in the list, it is added.
New Input: addOrRemove("apple") on the updated list [banana, cherry, apple]
Output: [banana, cherry]
Explanation: Since "apple" is now in the list, it is removed.
Notes:
a. Don’t forget to update the size.
b. Do not use any of the List interface methods in the implementation of these method
5. public string toString ()
Return a string representation of all items in the array list.
Example:
Input: toString() on list [banana, apple, cherry]
Output: "[banana, apple, cherry]"
Explanation: This method creates a string format for all items.
Page 2 of 3
Faculty of Science CSC 320L Lab2
6. public string evenToString ()
Return a string of items located at even indexes (0, 2, 4, …).
Example:
Input: evenToString() on list [banana, apple, cherry, date, elderberry]
Output: "[banana, cherry, elderberry]"
Explanation: The method includes only elements at indexes 0, 2, and 4.
Page 3 of 3