...

View Full Version : next() cannot override next() in my abstract list.



edd21
03-18-2012, 04:44 AM
I get an error when calling next() during my return SalesPerson method.
Here is the SortedListOfSalesperson:






public class SortedListOfSalesperson extends AbstractList
{
public SortedListOfSalesperson() { super(); }
public SortedListOfSalesperson(int maxItems) { super(maxItems); }

public boolean contains(Comparable otherItem)
// Returns true if item is in the list; false otherwise
{
SalesPerson item = (SalesPerson) otherItem;
int index = 0;
while (index < numItems && ((SalesPerson)listItems[index]).compareTo(item) > 0)
index++;
return (index < numItems && ((SalesPerson)listItems[index]).compareTo(item) == 0);
}

public SalesPerson next() { return (SalesPerson)super.next(); }

public void add(Comparable item)
// If the list is not full, puts item in its proper place in
// the list; otherwise list is unchanged
// Assumption: item is not already in the list
{
if (!isFull())
{
int index = numItems - 1; // Loop control variable
while (index >= 0 &&
(((SalesPerson)item).compareTo((SalesPerson)listItems[index]) < 0))
{ // Find addition point
listItems[index+1] = listItems[index];
index--;
}
listItems[index+1] = item; // Add item
numItems++; // Increment number of items
}
}

public void remove(Comparable otherItem)
// Removes item from the list if it is there. Implements "remove if there"
// semantics. Maintains ordering of elements
{
int index = 0;
boolean found = false;
SalesPerson item = (SalesPerson) otherItem;
while (index < numItems && !found)
{
if (((SalesPerson)listItems[index]).compareTo(item) == 0)
found = true;
else
index++;
}
if (found)
{
for (int count = index; count < numItems-1; count++)
listItems[count] = listItems[count+1];
numItems--;
}
}
}





And my abstract list class:





//****************************************************************
//Class AbstractList provides common methods to create and
//maintain a list of Comparable objects. Subclasses must
//implement its transformers with appropriate types and semantics
//****************************************************************
public abstract class AbstractList
{
protected Comparable[] listItems; // Array to hold list items
protected int numItems; // Number of items in the list
protected int currentPos; // State variable for iteration

public AbstractList()
// Instantiates an empty list object with room for 100 items
{
numItems = 0;
listItems = new Comparable[100];
currentPos = 0;
}

public AbstractList(int maxItems)
// Instantiates an empty list object with room for maxItems items
{
numItems = 0;
listItems = new Comparable[maxItems];
currentPos = 0;
}

// Returns true if there is no room for another component; false otherwise
public boolean isFull() { return (listItems.length == numItems); }

// Returns true if there are no components in the list; false otherwise
public boolean isEmpty() { return (numItems == 0); }

// Returns the number of components in the list
public int size() { return numItems; }

// Returns true if item is in the list; false otherwise
public abstract boolean contains(Comparable otherItem);

// Abstract transformers to be implement by subclasses
public abstract void add(Comparable item);
public abstract void remove(Comparable otherItem);

// Prepare list for iteration
public void resetList() { currentPos = 0; }

// Returns true if there is another item in the list; false otherwise
public boolean hasNext() { return (currentPos < numItems); }

public Comparable next()
// Returns the item at the currentPos position; resets current
// position to first item after the last item is returned
// Assumption: no transformers have been invoked since last call
{
Comparable next = listItems[currentPos];
currentPos++;
return next;
}
}

Fou-Lu
03-18-2012, 07:28 PM
Can't replicate the problem. What is the error?



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum