Skip to content

Instantly share code, notes, and snippets.

@danielgospodinow
Last active September 22, 2019 19:51
Show Gist options
  • Save danielgospodinow/0fe3dee5a7f96b25106958cac07cf401 to your computer and use it in GitHub Desktop.
Save danielgospodinow/0fe3dee5a7f96b25106958cac07cf401 to your computer and use it in GitHub Desktop.
Array List get operation
/**
* Finds and returns an element by its index.
*
* @tparam T type
* @param index index of the required element.
* @return the element required
*/
template<typename T>
T ArrayList<T>::get(const int index) const {
if (index < 0 || index >= _size) {
// If the index is out of bounds, throw an exception.
throw std::invalid_argument("Index out of bounds");
} else {
// Retrieve the element from the specified index.
return _array[index];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment