The method remove of the class ArrayListClass removes only
the first occurence of an element. Add the method removeAll as an abstract method to the class ArrayListClass that would remove all occurrences of a given element. Also, write the definition of the method removeAll for the class UnorderedArrayL ist and a program to test this method.
This is what i've done so far
public class UnorderedArrayL ist extends ArrayListClass
{
public UnorderedArrayL ist(int size)
{
super(size);
}
public UnorderedArrayL ist()
{
super();
}
//Copy constructor
public UnorderedArrayL ist(UnorderedAr rayList otherList)
{
super(otherList );
}
//Method to determine whether searchItem is in the list.
//Postcondition: If searchItem is found, returns the location
// in the array where the searchItem is found;
// otherwise, returns -1.
public int seqSearch(DataE lement searchItem)
{
int loc;
boolean found = false;
for(loc = 0; loc < length; loc++)
if(list[loc].equals(searchI tem))
{
found = true;
break;
}
if(found)
return loc;
else
return -1;
} //end seqSearch
//Method to remove an item from the list.
//The parameter removeItem specifies the item to
//be removed.
//Postcondition: If removeItem is found in the list, it is
// removed from the list and length is
// decremented by one.
public void remove(DataElem ent removeItem)
{
int loc;
if(length == 0)
System.err.prin tln("Cannot delete from an empty list.");
else
{
loc = seqSearch(remov eItem);
if(loc != -1)
removeAt(loc);
else
System.out.prin tln("The item to be deleted is "
+ "not in the list.");
}
} //end remove
public void removeAll(DataE lement removeItem)
{
int loc;
int locn;
boolean found = false;
if (length == 0)
System.err.prin tln("Cannot delete from an empty list");
else
{
loc = seqSearch(remov eItem);
for (loc = 0; loc < length; loc++)
if (list[loc].equals(removeI tem))
{
if (list[loc] == removeItem)
{
found = true;
locn = seqSearch(remov eItem);
for(locn = loc + 1; locn < length; locn++)
list[locn-1] = list[locn];
removeAt(loc);
removeAt(locn);
loc--;
}
else
break;
}
if (!found)
System.out.prin tln("The item to be deleted is " + " is not in the list");
}
}
}
the first occurence of an element. Add the method removeAll as an abstract method to the class ArrayListClass that would remove all occurrences of a given element. Also, write the definition of the method removeAll for the class UnorderedArrayL ist and a program to test this method.
This is what i've done so far
public class UnorderedArrayL ist extends ArrayListClass
{
public UnorderedArrayL ist(int size)
{
super(size);
}
public UnorderedArrayL ist()
{
super();
}
//Copy constructor
public UnorderedArrayL ist(UnorderedAr rayList otherList)
{
super(otherList );
}
//Method to determine whether searchItem is in the list.
//Postcondition: If searchItem is found, returns the location
// in the array where the searchItem is found;
// otherwise, returns -1.
public int seqSearch(DataE lement searchItem)
{
int loc;
boolean found = false;
for(loc = 0; loc < length; loc++)
if(list[loc].equals(searchI tem))
{
found = true;
break;
}
if(found)
return loc;
else
return -1;
} //end seqSearch
//Method to remove an item from the list.
//The parameter removeItem specifies the item to
//be removed.
//Postcondition: If removeItem is found in the list, it is
// removed from the list and length is
// decremented by one.
public void remove(DataElem ent removeItem)
{
int loc;
if(length == 0)
System.err.prin tln("Cannot delete from an empty list.");
else
{
loc = seqSearch(remov eItem);
if(loc != -1)
removeAt(loc);
else
System.out.prin tln("The item to be deleted is "
+ "not in the list.");
}
} //end remove
public void removeAll(DataE lement removeItem)
{
int loc;
int locn;
boolean found = false;
if (length == 0)
System.err.prin tln("Cannot delete from an empty list");
else
{
loc = seqSearch(remov eItem);
for (loc = 0; loc < length; loc++)
if (list[loc].equals(removeI tem))
{
if (list[loc] == removeItem)
{
found = true;
locn = seqSearch(remov eItem);
for(locn = loc + 1; locn < length; locn++)
list[locn-1] = list[locn];
removeAt(loc);
removeAt(locn);
loc--;
}
else
break;
}
if (!found)
System.out.prin tln("The item to be deleted is " + " is not in the list");
}
}
}
Comment