Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

public void add(E e); /** Add a new element at the specified index in this list

ID: 3668487 • Letter: P

Question

public void add(E e);

/** Add a new element at the specified index in this list */
public void add(int index, E e);

/** Clear the list */
public void clear();

/** Return true if this list contains the element */
public boolean contains(E e);

/** Return the element from this list at the specified index */
public E get(int index);

/** Return the index of the first matching element in this list.
* Return -1 if no match. */
public int indexOf(E e);

/** Return true if this list contains no elements */
public boolean isEmpty();

/** Return the index of the last matching element in this list
* Return -1 if no match. */
public int lastIndexOf(E e);

/** Remove the first occurrence of the element o from this list.
* Shift any subsequent elements to the left.
* Return true if the element is removed. */
public boolean remove(E e);

/** Remove the element at the specified position in this list
* Shift any subsequent elements to the left.
* Return the element that was removed from the list. */
public E remove(int index);

/** Replace the element at the specified position in this list
* with the specified element and returns the new set. */
public Object set(int index, E e);

/** Return the number of elements in this list */
public int size();

1. Implement the following method for MyLinkedList

Explanation / Answer

example of LinkedList along with brief description of it’s methods.

Output:

For all the examples in the below methods, consider llistobj as a reference for LinkedList<String>.

LinkedList<String> llistobj = new LinkedList<String>();

1) boolean add(Object item): It adds the item at the end of the list.

It would add the string “Hello” at the end of the linked list.

2) void add(int index, Object item): It adds an item at the given index of the the list.

This will add the string “bye” at the 3rd position( 2 index is 3rd position as index starts with 0).

3) boolean addAll(Collection c): It adds all the elements of the specified collection c to the list. It throws NullPointerException if the specified collection is null. Consider the below example –

This piece of code would add all the elements of ArrayList to the LinkedList.

4) boolean addAll(int index, Collection c): It adds all the elements of collection c to the list starting from a give index in the list. It throws NullPointerException if the collection c is null and IndexOutOfBoundsException when the specified index is out of the range.

It would add all the elements of the ArrayList to the LinkedList starting from position 6 (index 5).

5) void addFirst(Object item): It adds the item (or element) at the first position in the list.

It would add the string “text” at the beginning of the list.

6) void addLast(Object item): It inserts the specified item at the end of the list.

This statement will add a string “Chaitanya” at the end position of the linked list.

7) void clear(): It removes all the elements of a list.

8) Object clone(): It returns the copy of the list.

For e.g. My linkedList has four items: text1, text2, text3 and text4.

Output: The output of above code would be:

[text1, text2, text3, text4]

9) boolean contains(Object item): It checks whether the given item is present in the list or not. If the item is present then it returns true else false.

It will check whether the string “TestString” exist in the list or not.

10) Object get(int index): It returns the item of the specified index from the list.

It will fetch the 3rd item from the list.

11) Object getFirst(): It fetches the first item from the list.

12) Object getLast(): It fetches the last item from the list.

13) int indexOf(Object item): It returns the index of the specified item.

14) int lastIndexOf(Object item): It returns the index of last occurrence of the specified element.

integer variable pos will be having the index of last occurrence of string “hello”.

15) Object poll(): It returns and removes the first item of the list.

16) Object pollFirst(): same as poll() method. Removes the first item of the list.

17) Object pollLast(): It returns and removes the last element of the list.

18) Object remove(): It removes the first element of the list.

19) Object remove(int index): It removes the item from the list which is present at the specified index.

It will remove the 5th element from the list.

20) Object remove(Object obj): It removes the specified object from the list.

21) Object removeFirst(): It removes the first item from the list.

22) Object removeLast(): It removes the last item of the list.

23) Object removeFirstOccurrence(Object item): It removes the first occurrence of the specified item.

It will remove the first occurrence of the string “text” from the list.

24) Object removeLastOccurrence(Object item): It removes the last occurrence of the given element.

It will remove the last occurrence of string “String1.

25) Object set(int index, Object item): It updates the item of specified index with the give value.

It will update the 3rd element with the string “Test”.

26) int size(): It returns the number of elements of the list.

may be this could help you to find your solution