0% found this document useful (0 votes)
18 views3 pages

ArrayList Method Implementations

The document outlines the guidelines for Lab 2 of the CSC 320 course at Al Maaref University, focusing on the implementation of specific methods in the MUArrayList class. It details six methods to be implemented, including indexOf, lastIndexOf, numberOfOccurrences, addOrRemove, toString, and evenToString, along with examples and explanations for each. Students are instructed to submit only the Java files separately and refer to the provided files on Google Classroom.

Uploaded by

10121442
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

ArrayList Method Implementations

The document outlines the guidelines for Lab 2 of the CSC 320 course at Al Maaref University, focusing on the implementation of specific methods in the MUArrayList class. It details six methods to be implemented, including indexOf, lastIndexOf, numberOfOccurrences, addOrRemove, toString, and evenToString, along with examples and explanations for each. Students are instructed to submit only the Java files separately and refer to the provided files on Google Classroom.

Uploaded by

10121442
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

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

Common questions

Powered by AI

Implementing 'evenToString()' enhances the utility of an array list by allowing selective viewing and processing of elements located at even indexes. This can be particularly useful in applications requiring partitioned data representations, such as alternating sequences or buffer processing in computational tasks where even-indexed elements represent distinct data, enabling the methodical analysis or presentation of such data sets.

The 'addOrRemove(E item)' method checks whether the item is already present in the array. If the item is absent, it adds the item, and if present, it removes it, updating the size of the array list accordingly. This approach maintains data integrity by ensuring that the list does not contain duplicate entries of the same item. The method's logic effectively handles toggle-like operations, ensuring the list's state accurately reflects changes based on the presence or absence of the item.

'indexOf' and 'lastIndexOf' differ primarily in the direction and logic of their search within a list. 'indexOf' searches from the beginning of the list to find the first occurrence of an item, which is ideal for tasks needing the earliest instance of an element, such as chronological data processing. Conversely, 'lastIndexOf' starts searching from the end to find the last occurrence, which is beneficial when recent data overwrite previous information, such as log file analyses. Choosing between these depends on whether the context or problem prioritizes historical or recent data relevance.

Avoiding the use of List interface methods in the MUArrayList implementation enforces a detailed, hands-on manipulation of the underlying array data structure, thereby providing a deeper understanding of elementary operations such as searching, adding, and removing elements. This restriction promotes an appreciation of the complexities involved in list handling by making operations more transparent, ensuring students understand how these functionalities are executed at a lower level within the data structure itself.

To ensure the efficiency of the 'indexOf(E item)' method in the MUArrayList class, it is crucial to iterate through the array from the start and stop as soon as the item is found, using a loop. This approach leverages the zero-indexed nature of arrays to directly access elements, avoiding unnecessary comparisons beyond the point where the item is located, thereby optimizing the search operation.

You might also like