Question

Please help with the codes for the implementation of java.util.List, specifically for the 3 below overridden...

Please help with the codes for the implementation of java.util.List, specifically for the 3 below overridden methods

@Override

       public int lastIndexOf(Object arg0) { required code }

       @Override

       public ListIterator<R> listIterator() { required code }

       @Override

       public ListIterator<R> listIterator(int arg0) { required code }

PLEASE NOTE:

Below are some of other overridden methods that are already implemented, they are meant to be examples of what the answers to the above questions for the 3 methods should look like.

public void add(int index, R r) {

             if (index < 0 || index >= size()) {

                    throw new IndexOutOfBoundsException();

             }else if (index == size()-1) {

                    add(r);return;     

             }else if (index == 0) {

                    Node n = new Node(r, head);

                    head = n;

             }else {

                    Node temp = head;

                    for(int i = 0; i < index - 1; i++) {

                           temp = temp.getNext();

                    }

                    Node n = new Node(r, temp.getNext());

                    temp.setNext(n);

             }

             size++;

                   

       }

class Node {

            

             private R element;

             private Node next;

            

             public Node(R element, Node next) {

                    this.element = element;

                    this.next = next;

             }

            

            

             public R getElement() {

                    return element;

             }

             public void setElement(R element) {

                    this.element = element;

             }

             public Node getNext() {

                    return next;

             }

             public void setNext(Node next) {

                    this.next = next;

             }           

            

       }

      

      

       class LinkedListIterator implements Iterator {

            

             private Node n;

             private Node bn;

             private Node bbn;

             private boolean removed;

            

             public LinkedListIterator() {

                    n = head;

                    bn = null;

                    bbn = null;

                    removed = false;

             }

             @Override

             public boolean hasNext() {

                    return n != null;

             }

             @Override

             public R next() {

                    if (n == null) {

                           throw new NoSuchElementException();                 

                    }

                    R r = n.element;

                    bbn = bn;

                    bn = n;

                    n = n.next;

                    //n = n.getNext();

                    removed = false;

                    return r;

             }

0 0
Add a comment Improve this question Transcribed image text
Answer #1

codes for the implementation of java.util.List

Solution:

Pleas note : As per chegg guidelines, I am allowed to answere first four codes. So here are these methods:

1. public boolean addAll(Collection arg0)

// Java program to illustrate boolean addAll(Collection arg0)
import java.io.*;
import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String args[])
   {

    ArrayList<Integer> arrlist1 = new ArrayList<Integer>(5);

    arrlist1.add(10);
       arrlist1.add(20);
       arrlist1.add(35);

   System.out.println("Printing list1:");
       for (Integer number : arrlist1)
           System.out.println("Number = " + number);   
       ArrayList<Integer> arrlist2 =
                           new ArrayList<Integer>(5);
       arrlist2.add(26);
       arrlist2.add(32);
       arrlist2.add(31);
       arrlist2.add(38);


       System.out.println("Printing list2:");
       for (Integer number : arrlist2)
           System.out.println("Number = " + number);         

arrlist1.addAll(arrlist2);

       System.out.println("Printing all the elements");

       for (Integer number : arrlist1)
           System.out.println("Number = " + number);         
   }
}

2. public boolean addAll(int arg0, Collection arg1)

// Java program boolean addAll(int arg0, Collection arg1)
import java.io.*;
import java.util.ArrayList;

public class ArrayListDemo {
   public static void main(String args[])
   {
       ArrayList<Integer> arrlist =  new ArrayList<Integer>(5);
       arrlist.add(10);
       arrlist.add(20);
       arrlist.add(30);


       System.out.println("Printing list1:");
       for (Integer number : arrlist)
           System.out.println("Number = " + number);         
       ArrayList<Integer> arrlist2 = new ArrayList<Integer>(5);
       arrlist2.add(40);
       arrlist2.add(70);
       arrlist2.add(81);
       arrlist2.add(35);
       System.out.println("Printing list2:");
       for (Integer number : arrlist2)
           System.out.println("Number = " + number);         


       arrlist.addAll(2, arrlist2);

       System.out.println("Printing all the elements");
       for (Integer number : arrlist)
           System.out.println("Number = " + number);         
   }
}

3. public boolean containsAll(Collection arg0)

// Java code for containsAll() method
import java.util.*;

public class ListDemo {
   public static void main(String args[])
   {
   List<Integer> list = new ArrayList<Integer>();

//add elements into the list
       list.add(10);
       list.add(15);
       list.add(30);
       list.add(20);
       list.add(5);
       System.out.println("List: " + list);


       List<Integer> listTemp = new ArrayList<Integer>();

       listTemp.add(30);
       listTemp.add(15);
       listTemp.add(5);

       System.out.println("Are all the contents equal? " + list.containsAll(listTemp));
   }
}

4. public int lastIndexOf(Object arg0)

// Java code for lastIndexof() method

import java.util.ArrayList;
import java.util.Arrays;
public class ArrayListExample
{
public static void main(String[] args)
{
ArrayList<String> list = new ArrayList<>(Arrays.asList("alex", "brian", "devid","alex","Hennry","gary","alex","harry"));

int lastIndex = list.lastIndexOf("alex");

System.out.println(lastIndex);

lastIndex = list.lastIndexOf("hello");

System.out.println(lastIndex);
}
}


answered by: ANURANJAN SARSAM
Add a comment
Answer #2

I have given the implementations for the methods. I have just renamed arg0 to obj and index in the method paramater list.

@Override
public int lastIndexOf(Object obj) {
   int i = 0;
   int lastIndex = -1;
   Node temp = head;
   while(temp != null){
       if(temp.getElement().equals(obj)){
           lastIndex = i;
       }
       temp = temp.getNext();
       i++;
   }
   return lastIndex;
}


@Override
public ListIterator<R> listIterator() {
   return new LinkedListIterator();
}

@Override
public ListIterator<R> listIterator(int index) {
   if (index < 0 || index >= size())
       throw new IndexOutOfBoundsException();
   ListIterator<R> iter = listIterator();
   for(int i = 0 ; i < index && iter.hasNext(); i++){
       iter.next();
   }
   return iter;
}

Add a comment
Know the answer?
Add Answer to:
Please help with the codes for the implementation of java.util.List, specifically for the 3 below overridden...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need help with todo line please public class LinkedList { private Node head; public LinkedList()...

    I need help with todo line please public class LinkedList { private Node head; public LinkedList() { head = null; } public boolean isEmpty() { return head == null; } public int size() { int count = 0; Node current = head; while (current != null) { count++; current = current.getNext(); } return count; } public void add(int data) { Node newNode = new Node(data); newNode.setNext(head); head = newNode; } public void append(int data) { Node newNode = new Node(data);...

  • JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has...

    JAVA: Already completed: MyList.java, MyAbstractList.java, MyArrayList.java, MyLinkedLink.java, MyStack.java, MyQueue.java. Need to complete: ReversePoem.java. This program has you display a pessimistic poem from a list of phrases. Next, this program has you reverse the phrases to find another more optimistic poem. Use the following algorithm. 1.   You are given a list of phrases each ending with a pound sign: ‘#’. 2.   Create a single String object from this list. 3.   Then, split the String of phrases into an array of phrases...

  • Java - I need help creating a method that removes a node at the specific index...

    Java - I need help creating a method that removes a node at the specific index position. The * first node is index 0. public boolean delAt(int index) { src code 2 different classes ******************************************** public class Node { private String data; private Node next; public Node(String data, Node next) { this.data = data; this.next = next; } public Node() { } public String getData() { return data; } public void setData(String data) { this.data = data; } public void...

  • Given a singly-linked list interface and linked list node class, implement the singly-linked list which has...

    Given a singly-linked list interface and linked list node class, implement the singly-linked list which has the following methods in Java: 1. Implement 3 add() methods. One will add to the front (must be O(1)), one will add to the back (must be O(1)), and one will add anywhere in the list according to given index (must be O(1) for index 0 and O(n) for all other indices). They are: void addAtIndex(int index, T data), void addToFront(T data), void addToBack(T...

  • Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(),...

    Problem 2: based on java.util.LinkedList class, create MyStack class that should have the methods: push(), pop(), peek(), size(), isEmpty(). my linkedlist class is: public class Lab8_problem1 {    private Node head, tail; private int size;    public Lab8_problem1() { this.head = null; this.tail = null; this.size = 0; } //Insert a node at specifc Location public void insertAt(int value, int index) { Node newNode = new Node(); newNode.data = value; if(head == null){ head = newNode; tail = newNode; head.next...

  • iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains...

    iImplement a Singly Linked List detectLoop in Java, it would check whether the linked list contains a loop. Print true if yes, false if not. Test by using the following code: LL<Integer> L = new LL<>(); for (int i = 1000; i > 0; i-=3) sl.add(i); try { L.insert(122, L.getNode(70), L.getNode(21)); if (L.detectLoop()) System.out.println("True"); else System.out.println("False."); } catch(Exception e){ e.printStackTrace(); } class Linkedlist<E>{ private static class Node<E>{ private E element; private Node<E> next; public Node(E e, Node<E> n){ element =...

  • Need help completing my instance method for deleteFront() as specified below To the class IntegerLinkedList, add...

    Need help completing my instance method for deleteFront() as specified below To the class IntegerLinkedList, add an instance method deleteFront such that … Method deleteFront has no input. Method deleteFront returns … an empty Optional instance if the list is empty an Optional instance whose value is the integer that was deleted from the front of the list, otherwise Hints https://docs.oracle.com/javase/10/docs/api/java/util/Optional.html import java.util.Optional; public class IntegerLinkedList { private IntegerNode head ; private int numberOfItems ; public int getNumberOfItems() { return...

  • Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Nod...

    Complete the implementation of the method replace: public class SinglyLinkedList private Node head, public SinglyLinkedListo this(null) public SinglyLinkedList(Node head) [ this.head -head public Node getHeado return head public void setHead(Node head) [ head: this. head Method that creates a Node containing 'item' @param item Value to be added this.headnew Node(item, this.head) * and adds it as the first element in the list *I public void add(int item) Method that finds the node at the given index d replaces its value...

  • Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to under...

    Doubly Linked List Java Help Details: First, read the DoublyLinkedList.java code and try to understand what each field stores and what each method is doing. Modify and complete the class as described below •The field size was defined in the class but was never maintained. Set the current default value and modify it whenever it is needed in the existing methods and other methods you implement as it is needed. It should always include the number of Nodes inside the...

  • When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses...

    When compiling the LinkedList and Iterator class, the following error is being produced: Note: LinkedList.java uses unchecked or unsafe operations. Note: Recompile with -Xlint:unchecked for details. Any suggestions? public class LinkedList<T> {    Node<T> itsFirstNode;    Node<T> itsLastNode;    private int size;    public LinkedList() {        itsFirstNode = null;        itsLastNode = null;          size = 0;    }    public Iterator<T> getIterator() {        return new Iterator(this);    }    // THIS WILL NEED...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT