In Java, the List interface provides the following methods (including those inhe
ID: 3891271 • Letter: I
Question
In Java, the List interface provides the following methods (including those inherited from Collection): add, addAll, clear, contains, containsAll, equals, isEmpty, remove, removeAll, retain, size, toArray, get, indexOf, lastIndexOf, listIterator, set. Of these, which would you select to implement if you were going to implement your own ordered list? Specifically, the list will be used to insert, delete and search for items as well as being able to traverse the list to output the entire thing. Only make reference to the above methods, not methods not listed here. Provide a brief explanation for why you are including the ones you select.
Explanation / Answer
add -> used add elements individually whenever required as list is our own ordered
clear -> to delete all elements in a list
contains -> to search element is present in the list or not
isEmpty -> whenever we traversing an list by removing an elements in list
remove -> to delete an element in a list
retainAll -> to delete all elements expect selected elements
size -> whenever traversing an list with loops without using isEmpty or hasNext methods
toArray -> to access elements faster if needed only in required conditions it is used
get -> to search an element or to get the value of element by index in a list
indexOf -> to identify first occurance of element in list whenever repeated values are present or not
lastIndexOf -> to identify last occurance of element in list whenever repeated values are present or not
listIterator -> to traverse the list directly
set -> modifiying list values without adding or removing elements. It overrrides the element value at particular index
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.