public String middleElement(ArrayList<String> list){// Implement this method}
Error
Errors:
Description
Write a method that returns the middle element of an ArrayList of Strings.
If there are an odd number of elements in the ArrayList, return the element in the middle of the array.
For example if we had an ArrayList<String> list
with the elements ["hello", "world", "!"]
:
middleElement(list)
Should return:
"world"
If there are an even number of elements in the ArrayList, return the second of the two middle elements.
For example if we had an ArrayList<String> list
with the elements ["my", "name", "is", "Karel"]
:
middleElement(list)
Should return:
"is"
If there is are no elements in the ArrayList, return the empty String “”.
For example:
middleElement(new ArrayList<String>())
Should return:
""
Test Cases
Pass | Test | Message | |
---|---|---|---|
- | Test results will show here. | - |
Errors:
Write a method that returns the middle element of an ArrayList of Strings.
If there are an odd number of elements in the ArrayList, return the element in the middle of the array.
For example if we had an ArrayList<String> list
with the elements ["hello", "world", "!"]
:
middleElement(list)
Should return:
"world"
If there are an even number of elements in the ArrayList, return the second of the two middle elements.
For example if we had an ArrayList<String> list
with the elements ["my", "name", "is", "Karel"]
:
middleElement(list)
Should return:
"is"
If there is are no elements in the ArrayList, return the empty String “”.
For example:
middleElement(new ArrayList<String>())
Should return:
""