////////MyArrayList.java///////////////////
import java.util.*;
class MyArrayList implements MyList
{
private ArrayList data;
final int NOT_FOUND = -1;
private int incIndex = 0;
public void clear()
{
data.clear();
}
//Returns the current number of elements in the list.
public int size()
{
return data.size();
}
//Returns true if no elements, false otherwise.
public boolean isEmpty()
{
return data.isEmpty();
}
//Returns true if this list contains the specified element,
//false otherwise.
public boolean contains(Object obj)
{
return data.contains(obj);
}
//Adds element to end of list. Increases size() by 1.
//Only need to return true, since this assignment requires that this
//operation always succeeds.
public boolean add(Object obj)
{
return data.add(obj);
}
//Removes first occurrence of element if present.
//Returns true if collection modified (size decreases by one),
//false otherwise.
public boolean remove(Object obj)
{
incIndex = data.indexOf(obj);
if(incIndex != NOT_FOUND)
{
data.remove(incIndex);
return true;
}
else
{
return false;
}
}
//Returns a MyIterator object, default initialized to refer to the
//first element.
/*public MyIterator iterator()
{
return new MyArrayListIterator(this);
}
private class MyArrayListIterator implements MyIterator
{
private ListIterator AL;
public MyArrayListIterator(List myList)
{
AL = myList.listIterator();
}
//Returns true if the iterator has more elements.
public boolean hasNext()
{
return(cursor<AL.size());
}
//Returns the next element in the list.
public Object next()
{
return AL.get(cursor++);
}
//Removes from the underlying collection the last element
//returned by the iterator (its currently positioned element).
public void remove()
{
AL.remove(AL.get(cursor));
}
}*/
//Returns the element at index, null if not present or
//index is out of range.
public Object get(int index)
{
if(index >= 0 && index < data.size())
{
return data.get(new Integer(index));
}
else
{
return null;
}
}
//Replaces the element at the specified index in this list
//with the specified element.
//Returns the original element at index, null if not present
//or index in out of range.
public Object set(int index, Object obj)
{
return data.set(index,obj);
}
//Returns the index in this list of the first occurrence of the
//specified element, or -1 if this list does not contain this element.
public int indexOf(Object obj)
{
return data.indexOf(obj);
}
}
//////MyList.java///////////////////
interface MyList
{
//Removes all of the elements from this list.
//Deallocates all existing dynamically allocated memory.
//size() goes to zero.
public void clear();
//Returns the current number of elements in the list.
public int size();
//Returns true if no elements, false otherwise.
public boolean isEmpty();
//Returns true if this list contains the specified element,
//false otherwise.
public boolean contains(Object o);
//Adds element to end of list. Increases size() by 1.
//Only need to return true, since this assignment requires that this
//operation always succeeds.
public boolean add(Object o);
//Removes first occurrence of element if present.
//Returns true if collection modified (size decreases by one),
//false otherwise.
public boolean remove(Object o);
//Returns a MyIterator object, default initialized to refer to the
//first element.
//public MyIterator iterator();
//Returns the element at index, null if not present or
//index is out of range.
public Object get(int index);
//Replaces the element at the specified index in this list
//with the specified element.
//Returns the original element at index, null if not present
//or index in out of range.
public Object set(int index, Object o);
//Returns the index in this list of the first occurrence of the
//specified element, or -1 if this list does not contain this element.
public int indexOf(Object o);
}
///////////////TestMyList2.java/////////////////////
import java.io.*;
class TestMyList2 {
public static void main(String args[])
{
System.out.println("----------- ARRAY TEST --------------");
MyList alist = new MyArrayList();
testList(alist);
/* System.out.println("----------- LINK LIST TEST --------------");
MyList llist = new MyLinkList();
testList(llist);*/
}
static void testList(MyList ml)
{
PrintStream o = System.out;
if(ml.size() != 0 || !ml.isEmpty())
o.println("Logic error " + 1);
ml.clear();
if(ml.size() != 0 || !ml.isEmpty())
o.println("Logic error " + 2);
if(ml.contains(new Double(3.4)))
o.println("Logic error " + 3);
for(int i = 0; i < 1000; i++)
ml.add(new Integer(i));
if(ml.size() != 1000 || ml.isEmpty())
o.println("Logic error " + 4);
ml.clear();
if(ml.size() != 0 || !ml.isEmpty())
o.println("Logic error " + 5);
for(int i = 0; i < 2000; i++)
ml.add(new Integer(i));
for(int i = 0; i < 2000; i++) {
if(!ml.contains(new Integer(i)))
o.println("Logic error " + 6);
}
if(!ml.remove(new Integer(0)))
o.println("Logic error " + 7);
if(ml.size() != 1999)
o.println("Logic error " + 8);
if(!ml.remove(new Integer(1999)))
o.println("Logic error " + 9);
if(ml.size() != 1998)
o.println("Logic error " + 10);
ml.clear();
ml.add(new Integer(3));
ml.add(new Double(3.4));
ml.add(new Short((short)9));
if(ml.size() != 3)
o.println("Logic error " + 11);
Double d1 = (Double)ml.get(1);
if(d1 == null || !d1.equals(new Double(3.4)))
o.println("Logic error " + 12);
d1 = (Double)ml.set(1,new Long(234));
Long l1 = (Long)ml.get(1);
if(d1 == null || l1 == null ||
!d1.equals(new Double(3.4)) || !l1.equals(new Long(234)))
o.println("Logic error " + 13);
if(ml.indexOf(new String("not present")) != -1)
o.println("Logic error " + 14);
if(ml.indexOf(new Short((short)9)) != 2)
o.println("Logic error " + 15);
o.println("Test MyIterator:");
/*MyIterator it = ml.iterator();
for(int i = 0; it.hasNext(); i++)
o.println(i + " " + it.next());
it = ml.iterator();
if(ml.size() != 3 || !it.hasNext())
o.println("Logic error " + 16);
it.next(); // new call, added for this assignment
it.remove();
if(ml.size() != 2 || !it.hasNext())
o.println("Logic error " + 17);
it.next(); // advance to next (last)
it.remove();
// next statement changed for this assignment. was
// it.hasNext() for previous assignment
if(ml.size() != 1 || !it.hasNext())
o.println("Logic error " + 18);*/
}
}
public MyIterator iterator()
{
return new MyArrayListIterator(this);
}
private class MyArrayListIterator implements MyIterator
{
private ListIterator AL;
public MyArrayListIterator(MyList myList)
{
AL = myList.listIterator();
}
import java.util.*;
class MyArrayList implements MyList
{
private ArrayList data = new ArrayList();
final int NOT_FOUND = -1;
private int incIndex = 0;
public void clear()
{
data.clear();
}
//Returns the current number of elements in the list.
public int size()
{
return data.size();
}
//Returns true if no elements, false otherwise.
public boolean isEmpty()
{
return data.isEmpty();
}
//Returns true if this list contains the specified element,
//false otherwise.
public boolean contains(Object obj)
{
return data.contains(obj);
}
//Adds element to end of list. Increases size() by 1.
//Only need to return true, since this assignment requires that this
//operation always succeeds.
public boolean add(Object obj)
{
return data.add(obj);
}
//Removes first occurrence of element if present.
//Returns true if collection modified (size decreases by one),
//false otherwise.
public boolean remove(Object obj)
{
incIndex = data.indexOf(obj);
if(incIndex != NOT_FOUND)
{
data.remove(incIndex);
return true;
}
else
{
return false;
}
}
//Returns a MyIterator object, default initialized to refer to the
//first element.
public MyIterator iterator()
{
return new MyArrayListIterator(this);
}
private class MyArrayListIterator implements MyIterator
{
private ListIterator iter;
private boolean pending;
public MyArrayListIterator(List l)
{
iter = l.listIterator();
}
//Returns true if the iterator has more elements.
public boolean hasNext()
{
pending = true;
while (iter.hasNext())
{
return true;
}
return false;
}
//Returns the next element in the list.
public Object next()
{
if (!pending) hasNext();
pending = false;
return iter.next();
}
//Removes from the underlying collection the last element
//returned by the iterator (its currently positioned element).
public void remove()
{
iter.remove();
}
}
//Returns the element at index, null if not present or
//index is out of range.
public Object get(int index)
{
if(index >= 0 && index < data.size())
{
return data.get(new Integer(index));
}
else
{
return null;
}
}
//Replaces the element at the specified index in this list
//with the specified element.
//Returns the original element at index, null if not present
//or index in out of range.
public Object set(int index, Object obj)
{
return data.set(index,obj);
}
//Returns the index in this list of the first occurrence of the
//specified element, or -1 if this list does not contain this element.
public int indexOf(Object obj)
{
return data.indexOf(obj);
}
}
public MyIterator iterator()
{
return new MyArrayListIterator(this);
}
private class MyArrayListIterator implements MyIterator
{
private ListIterator AL;
private boolean pending;
public MyArrayListIterator(MyList myList)
{
AL = myList.listIterator();
}
public MyIterator iterator(){ return new MyArrayListIterator();}
private class MyArrayListIterator implements MyIterator
{
private ListIterator AL;
public void MyArrayListIterator()
{
AL=data.listIterator();
}
public boolean hasNext()
{
return AL.hasNext();
}
public Object next()
{
return AL.next();
}
public void remove()
{
AL.remove();
}
}
import java.util.*;
class MyArrayList implements MyList
{
private List data = new ArrayList();
final int NOT_FOUND = -1;
private int incIndex = 0;
public void clear()
{
data.clear();
}
//Returns the current number of elements in the list.
public int size()
{
return data.size();
}
//Returns true if no elements, false otherwise.
public boolean isEmpty()
{
return data.isEmpty();
}
//Returns true if this list contains the specified element,
//false otherwise.
public boolean contains(Object obj)
{
return data.contains(obj);
}
//Adds element to end of list. Increases size() by 1.
//Only need to return true, since this assignment requires that this
//operation always succeeds.
public boolean add(Object obj)
{
return data.add(obj);
}
//Removes first occurrence of element if present.
//Returns true if collection modified (size decreases by one),
//false otherwise.
public boolean remove(Object obj)
{
incIndex = data.indexOf(obj);
if(incIndex != NOT_FOUND)
{
data.remove(incIndex);
return true;
}
else
{
return false;
}
}
//Returns a MyIterator object, default initialized to refer to the
//first element.
public MyIterator iterator()
{
return new MyArrayListIterator();
}
private class MyArrayListIterator implements MyIterator
{
private ListIterator AL;
public MyArrayListIterator()
{
AL=data.listIterator();
}
//Returns true if the iterator has more elements.
public boolean hasNext()
{
return AL.hasNext();
}
//Returns the next element in the list.
public Object next()
{
return AL.next();
}
//Removes from the underlying collection the last element
//returned by the iterator (its currently positioned element).
public void remove()
{
AL.remove();
}
}
//Returns the element at index, null if not present or
//index is out of range.
public Object get(int index)
{
return data.get(new Integer(index));
}
//Replaces the element at the specified index in this list
//with the specified element.
//Returns the original element at index, null if not present
//or index in out of range.
public Object set(int index, Object obj)
{
return data.set(index,obj);
}
//Returns the index in this list of the first occurrence of the
//specified element, or -1 if this list does not contain this element.
public int indexOf(Object obj)
{
return data.indexOf(obj);
}
}
Usage: java [-options] class [args...]
      (to execute a class)
  or  java [-options] -jar jarfile [args...]
      (to execute a jar file)
where options include:
  -client    to select the "client" VM
  -server    to select the "server" VM
  -hotspot    is a synonym for the "client" VM  [deprecated]
         The default VM is client.
  -cp <class search path of directories and zip/jar files>
  -classpath <class search path of directories and zip/jar files>
         A ; separated list of directories, JAR archives,
         and ZIP archives to search for class files.
  -D<name>=<value>
         set a system property
  -verbose[:class|gc|jni]
         enable verbose output
  -version    print product version and exit
  -version:<value>
         require the specified version to run
  -showversion  print product version and continue
  -jre-restrict-search | -jre-no-restrict-search
         include/exclude user private JREs in the version search
  -? -help    print this help message
  -X       print help on non-standard options
  -ea[:<packagename>...|:<cl
  -enableassertions[:<packag
         enable assertions
  -da[:<packagename>...|:<cl
  -disableassertions[:<packa
         disable assertions
  -esa | -enablesystemassertions
         enable system assertions
  -dsa | -disablesystemassertions
         disable system assertions
  -agentlib:<libname>[=<opti
         load native agent library <libname>, e.g. -agentlib:hprof
          see also, -agentlib:jdwp=help and -agentlib:hprof=help
  -agentpath:<pathname>[=<op
         load native agent library by full pathname
  -javaagent:<jarpath>[=<opt
         load Java programming language agent, see java.lang.instrument
  -splash:<imagepath>
         show splash screen with specified image
Press any key to continue . . .