Question
3. Include a toString() method in the ArrayList class which will produce a String representation of this List. (Why is this method not needed in
3. Include a toString() method in the ArrayList class which will produce a String representation
of this List. (Why is this method not needed in the List interface?) 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 the Driver 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 can include the first value in your result
before entering the loop. > > > CODE TO BE MODIFIED BELOW:: > > >
LIST INTERFACE
package list;
public interface List
int size();
/** Clear this List */
void clear();
/** @return true iff this List is empty */
boolean isEmpty();
} >>>> ARRAYLIST CLASS
package list;
public class ArrayList
{
//instance variables
private int size = 0;
private E[] values;
//default constructor
public ArrayList()
{
this (10);
}
//parametrized constructor
public ArrayList(int cap)
{
values = (E[]) new Object[cap];
}
//implementing abstract functions from interface
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)
return true;
return false;
}
public void clear()
{
for(int i=0;i values[i]=(E)new Object(); size=0; } } >>>>>> NODE CLASS package list; public class Node package list; public class LinkedList } >>>>> DRIVERARRAYLIST CLASS package listDriver; import list.*; public class DriverArrayList { /** * This main method tests the ArrayList class */ public static void main (String [] args) { List 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 problem 2 // 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 problem 3 // System.out.println (" 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"); } }
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started