Programming and Problem Solving with Java

(やまだぃちぅ) #1

736


// Remove all copies of item from the list
{
intindex = 0 ;
while(index < numItems && item.compareTo(listItems[index]) <= 0 )
{
if(listItems[index].compareTo(item) == 0 )
{
for(intcount = index; count < numItems- 1 ; count++)
listItems[count] = listItems[count+ 1 ];
numItems--;
}
else
index++;
}
}
e.If a binary search is used to find the item to delete, you do not know whether
the one found is the first duplicate, the last duplicate, or one in the middle. The
method would have to search both before and after the item to look for
duplicate copies. It is easier to use a linear search.
9.public voidinsert(String item)
// If the list is not full, put item in its proper position in the
// list; otherwise, list is unchanged
{
booleanplaceFound = false;
intindex = 0 ;
if(!isFull())
{
while(!placeFound && index < numItems)
if(item.compareTo(listItems[index]) > 0 )
index++;
else
placeFound = true;
for(intcount = numItems; count > index; count--)
listItems[count] = listItems[count - 1 ];
listItems[index] = item;
numItems++;
}
}
Free download pdf