troubleshooting Question

ArrayList Interface Java Collections Error

Avatar of yunji1
yunji1 asked on
Java
40 Comments3 Solutions832 ViewsLast Modified:
I cannot figure out why this doesn't work. I need to implement the Java ArrayList Collections into my existing program methods. I keep getting a wierd compile error and it doesn't run. The teacher states that I don't need to handle exceptions.

Here is the error:
Note: C:\NEU\JAVA\HW8\test\MyArrayList.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.

Tool completed successfully

The
////////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);*/
 
    }
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 3 Answers and 40 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 3 Answers and 40 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros