KingJaymz
March 2nd, 2004, 11:03 AM
Hello all. I was hoping that you could help me. I have to write a merge method that merges a new sorted list to the current list. The new list is the only parameter of the method. So this is my question. I should probably know the answer, but how can I compare the parameter to the existing list? They're both entitled list, so I'm slightly confused. This is the code, if it will help, but I'm nowhere near finished with the merge method.
package ch03.genericLists;
public class SortedList extends List
{
public SortedList(int maxItems)
// Instantiates and returns a reference to an empty list object with room for maxItems elements.
{
super(maxItems);
}
public SortedList()
// Instantiates and returns a reference to an empty list object with room for 100 elements.
{
super(100);
}
public boolean isThere (Listable item)
// Returns true if an element with the same key as item is on this list, otherwise returns false.
{
int compareResult;
int midPoint;
int first = 0;
int last = numItems - 1;
boolean moreToSearch = (first <= last);
boolean found = false;
while (moreToSearch && !found)
{
midPoint = (first + last) / 2;
compareResult = item.compareTo(list[midPoint]);
if (compareResult == 0)
found = true;
else if (compareResult < 0) // item is less than element at location
{
last = midPoint - 1;
moreToSearch = (first <= last);
}
else // item is greater than element at location
{
first = midPoint + 1;
moreToSearch = (first <= last);
}
}
return found;
}
public Listable retrieve(Listable item)
// Returns a copy of the list element with the same key as item
{
int compareResult;
int first = 0;
int last = numItems - 1;
int midPoint = (first + last) / 2;
boolean found = false;
while (!found)
{
midPoint = (first + last) / 2;
compareResult = item.compareTo(list[midPoint]);
if (compareResult == 0)
found = true;
else if (compareResult < 0) // item is less than element at location
last = midPoint - 1;
else // item is greater than element at location
first = midPoint + 1;
}
return list[midPoint].copy();
}
public void insert (Listable item)
// Adds a copy of an item to this list.
{
int location = numItems - 1;
boolean moreToSearch = (location > -1);
while (moreToSearch)
{
if (item.compareTo(list[location]) > 0) // item is greater
moreToSearch = false;
else // item is less
{
list[location + 1] = list[location];
location--;
moreToSearch = (location > -1);
}
}
list[location + 1] = item;
numItems++;
}
public void delete (Listable item)
// Deletes the element that matches item from this list.
{
int location = 0;
while (item.compareTo(list[location]) != 0)
location++;
for (int index = location + 1; index < numItems; index++)
list[index - 1] = list[index];
numItems--;
}
public void merge(SortedList list)
{
int newSize = numItems + ;
int location;
boolean moreToSearch;
}
}
Please let me know if you need any more information. Thank you. Your help is very much appreciated.
package ch03.genericLists;
public class SortedList extends List
{
public SortedList(int maxItems)
// Instantiates and returns a reference to an empty list object with room for maxItems elements.
{
super(maxItems);
}
public SortedList()
// Instantiates and returns a reference to an empty list object with room for 100 elements.
{
super(100);
}
public boolean isThere (Listable item)
// Returns true if an element with the same key as item is on this list, otherwise returns false.
{
int compareResult;
int midPoint;
int first = 0;
int last = numItems - 1;
boolean moreToSearch = (first <= last);
boolean found = false;
while (moreToSearch && !found)
{
midPoint = (first + last) / 2;
compareResult = item.compareTo(list[midPoint]);
if (compareResult == 0)
found = true;
else if (compareResult < 0) // item is less than element at location
{
last = midPoint - 1;
moreToSearch = (first <= last);
}
else // item is greater than element at location
{
first = midPoint + 1;
moreToSearch = (first <= last);
}
}
return found;
}
public Listable retrieve(Listable item)
// Returns a copy of the list element with the same key as item
{
int compareResult;
int first = 0;
int last = numItems - 1;
int midPoint = (first + last) / 2;
boolean found = false;
while (!found)
{
midPoint = (first + last) / 2;
compareResult = item.compareTo(list[midPoint]);
if (compareResult == 0)
found = true;
else if (compareResult < 0) // item is less than element at location
last = midPoint - 1;
else // item is greater than element at location
first = midPoint + 1;
}
return list[midPoint].copy();
}
public void insert (Listable item)
// Adds a copy of an item to this list.
{
int location = numItems - 1;
boolean moreToSearch = (location > -1);
while (moreToSearch)
{
if (item.compareTo(list[location]) > 0) // item is greater
moreToSearch = false;
else // item is less
{
list[location + 1] = list[location];
location--;
moreToSearch = (location > -1);
}
}
list[location + 1] = item;
numItems++;
}
public void delete (Listable item)
// Deletes the element that matches item from this list.
{
int location = 0;
while (item.compareTo(list[location]) != 0)
location++;
for (int index = location + 1; index < numItems; index++)
list[index - 1] = list[index];
numItems--;
}
public void merge(SortedList list)
{
int newSize = numItems + ;
int location;
boolean moreToSearch;
}
}
Please let me know if you need any more information. Thank you. Your help is very much appreciated.