Menu

(Solved) : 3 Include Tostring Method Arraylist Class Produce String Representation List Method Needed Q44025058 . . .

3. Include a toString() method in the ArrayList class which willproduce a String representation

of this List. (Why is this method not needed in the Listinterface?) The items in the List should

be separated by commas, and enclosed in square brackets:[a,b,c]

Note that there is no comma after the last value.

An empty List should appear this way: [ ]

Test your solution by uncommenting the appropriate lines of theDriver used in problems 1 and 2.

/** @return this List as a String */

public String toString();

Hint: Handle the empty List as a special case. Then you caninclude the first value in your result

before entering the loop.
>
>
>

CODE TO BE MODIFIED BELOW::
>
>
>

LIST INTERFACE

package list;

public interface List <E>
{
/**
*
* @param 0 <= ndx <= size
* @return the value at the given index in this list
*/
E get (int ndx);
  
/**
* Set the value of the given index to the given value
*
* @param 0 <= ndx <= size
* @return the old value
*/
E set (int ndx, E value);
  
/**
* Add the given value at the end of this list
*
*/
void add (E value);
  
/**
* Insert the given value at the given index
* @param 0 <= ndx <= size
*/
void add(int ndx, E value);
  
/**
* Remove the value of the given index from this list
* @param 0 <= ndx < size
*/
  
E remove (int ndx);

/** @return the size of this List */

int size();

/** Clear this List */

void clear();

/** @return true iff this List is empty */

boolean isEmpty();

}

>>>>

ARRAYLIST CLASS

package list;

public class ArrayList <E> implements List <E>

{

   //instance variables

   private int size = 0;

   private E[] values;

   //default constructor

   public ArrayList()

   {

       this (10);

   }

   //parametrized constructor

   public ArrayList(int cap)

   {

       values = (E[]) newObject[cap];

   }

   //implementing abstract functions frominterface

   public E get (int ndx)

   {

       return values[ndx];

   }

   public E set(int ndx, E value)

   {

       E result = values[ndx];

       values[ndx] = value;

       return result;

   }

   public void add(E value)

   {

       add(size,value);

   }

   public void add(int ndx, E value)

   {

       if (values.length == size)

           alloc();

       for(int i = size; i > ndx;i–)

           values[i] =values[i-1];

       values[ndx] = value;

       size++;

   }

   private void alloc()

   {

       E[] tempArray =

              (E[]) new Object[2*values.length];

       for(int i = 0; i < size;i++)

           tempArray[i]= values [i];

       values = tempArray;

   }

   public E remove(int ndx)

   {

       E result = values[ndx];

       for(int i = ndx; i < size-1;i++)

           values[i] =values [i + 1];

       size–;

       return result;

   }

   public int size()

   {

       return size;

   }

   public boolean isEmpty()

   {

       if(size==0)

           returntrue;

       return false;

   }

   public void clear()

   {

       for(int i=0;i<size;i++)

          values[i]=(E)new Object();

       size=0;

   }

}

>>>>>>

NODE CLASS

package list;

public class Node <E>
{
   E value;
   Node <E> next;
   Node <E> prev;
  
   Node(E value, Node<E> next, Node<E>prev)
   {
       this.value = value;
       this.next = next;
       this.prev = prev;
   }
}

>>>>>>>

LINKED LIST CLASS

package list;

public class LinkedList <E> implements List<E>
{
   int size = 0;
   Node<E> head = new Node<E> (null, null,null);
   Node<E> tail = new Node<E> (null, null,head);
   private Node<E> ref;
  
   private void setRef(int ndx)
   {
       ref = head.next;
       for (int i = 0; i < ndx;i++)
           ref =ref.next;
   }
   public LinkedList()
   {
       head.next = tail;
   }
  
   public void add (E value)
   {
       Node<E> temp = newNode<E> (value, tail, tail.prev);
       tail.prev.next = temp;
       tail.prev = temp;
   }
  
   public void add(int ndx, E value)
   {
       Node<E> ref =head.next;
       setRef(ndx);
       Node<E> temp = newNode<E> (value, ref, ref.prev);
       ref.prev.next = temp;
       ref.prev = temp;
       size++;
   }
  
   public E get(int ndx)
   {
       Node<E> ref =head.next;
       setRef(ndx);
       return ref.value;
   }
  
   public E set(int ndx, E value)
   {
       setRef(ndx);
       E result = ref.value;
       ref.value = value;
       return result;
   }
  
   public E remove(int ndx)
   {
       setRef(ndx);
       ref.next.prev = ref.prev;
       ref.prev.next = ref.next;
       size–;
       return ref.value;
   }
  
   public int size()
   {
       return size;
   }
  
   public void clear()
   {
      System.out.println(“Cleared”);
   }
  
   public boolean isEmpty()
   {
       return false;
   }

}

>>>>>

DRIVERARRAYLIST CLASS

package listDriver;

import list.*;

public class DriverArrayList

{

/**

* This main method tests the ArrayList class

*/

public static void main (String [] args)

{ List<String> friends = new ArrayList <String>();

System.out.println (“Testing problem 1”);

friends.add (“joe”);

friends.add (“mary”);

friends.add (“jim”);

friends.add (“joe”); // Lists may contain duplicate

elements

friends.add (2, “sally”); // Insert at position 2

friends.remove (0); // Remove joe at position 0

if (friends.size() != 4)

System.err.println (“Error in add, remove or size”);

String s1 = “sal”;

String s2 = “ly”; // s1 + s2 is “sally”

System.out.println (“sally is at position ” + friends.indexOf(s1+ s2)); //

should be 1

if (friends.indexOf(s1+s2) != 1)

System.err.println (“Error in indexOf”);

//////// Uncomment the following lines when ready for problem2

// if (friends.contains (“Jim”))

// System.err.println (“Not correct”);

// if (!friends.contains (“jim”))

// System.err.println (“Not correct”);

// friends.add (“mary”);

// if (friends.indexOf(“mary”) != 0)

// System.err.println (“Not correct”);

////////////// Uncomment the following when ready for problem3

// System.out.println (“nn Testing problem 3”);

// System.out.println (“The list of friends is ” + friends);

// friends.clear();

// if (! friends.isEmpty())

// System.err.println (“Error in clear or isEmpty”);

// System.out.println (friends);

// for (int i=0; i< 25; i++)

// friends.add (“str” + i);

// System.out.println (friends);

// // Testing for efficiency

// for (int i=0; i<500000; i++)

// friends.add (“str” + i);

// for (int i=0; i<100000; i++)

// if (friends.indexOf(new String(“str3”)) != 3)

// System.err.println (“Not correct”);

// System.out.println (“Testing finished”);

}

}

Expert Answer


Answer to 3. Include a toString() method in the ArrayList class which will produce a String representation of this List. (Why is t…

OR