Programming and Problem Solving with Java

(やまだぃちぅ) #1
735

{

if(!isFull())
{
listItems[numItems] = item;
numItems++;
}
}
public voiddelete(String item)
// Remove all copies of item from the list
{
intindex = 0 ;
while(index < numItems)
{
if(listItems[index].compareTo(item) == 0 )
{
for(intcount = index; count < numItems- 1 ; count++)
listItems[count] = listItems[count+ 1 ];
numItems--;
}
else
index++;
}
}
}
d.Only the insertand deleteoperations are shown because they are the only
ones that would change with respect to part c.
public voidinsert(String item)
// If the list is not full, put item in its proper position in
// the list; otherwise, list is unchanged
{
if(!isFull())
{
intindex = numItems - 1 ; // Loop control variable
while(index >= 0 && (item.compareTo(listItems[index]) < 0 ))
{
listItems[index+ 1 ] = listItems[index]; // Find insertion point
index--;
}
listItems[index+ 1 ] = item; // Insert item
numItems++; // Increment number of items
}
}
public voiddelete(String item)

Free download pdf