Java 如何将逻辑添加到排序数组删除算法中,以说明;“未找到节点”;脚本

Java 如何将逻辑添加到排序数组删除算法中,以说明;“未找到节点”;脚本,java,arrays,algorithm,sorted,Java,Arrays,Algorithm,Sorted,我被指派为我的排序数组结构编写一个删除算法。对于我的程序来说,这仍然是一个问题的来源,因为在某些情况下,它正在完成删除,而对于“node not found”,它应该返回False。因此,delete函数可以工作,但即使找不到节点,它也可以工作。我的逻辑缺陷在哪里 public boolean delete(String targetListing) { int low = 0; int high = next - 1; int i

我被指派为我的排序数组结构编写一个删除算法。对于我的程序来说,这仍然是一个问题的来源,因为在某些情况下,它正在完成删除,而对于“node not found”,它应该返回False。因此,delete函数可以工作,但即使找不到节点,它也可以工作。我的逻辑缺陷在哪里

    public boolean delete(String targetListing)
    {
        int low = 0;
        int high = next - 1;
        int i = (low + high) / 2;

        //check to see if target listing is the same as the current node key field
        while(data[i].getName().compareToIgnoreCase(targetListing) != 0 && high != low)   
        {
            if(data[i].getName().compareToIgnoreCase(targetListing) >= 1)
            {
                high = i - 1; //eliminate the bottom of the array
            }
            else
            {
                low = i + 1; //eliminate the top of the array
            }

            i = (low + high) / 2;  
        }
        //this is my logic to determine if the node was found or not
        //I also tried if(low == high) but sometimes that would be true at the 
        //at the position that the node was found      
        if(i == next || i < 0)
        {
            return false; //node not found
        }

        for(int j = i; j < next - 1; j++)
        {
            data[j] = data[j + 1];
        }

        next = next - 1;
        data[next] = null;
        return true;//node found and deleted
公共布尔删除(字符串targetListing)
{
int低=0;
int高=下一个-1;
int i=(低+高)/2;
//检查目标列表是否与当前节点键字段相同
while(数据[i].getName().CompareTIgnoreCase(targetListing)!=0&&high!=low)
{
如果(数据[i].getName().compareToIgnoreCase(targetListing)>=1)
{
high=i-1;//消除数组的底部
}
其他的
{
low=i+1;//消除数组的顶部
}
i=(低+高)/2;
}
//这是我确定是否找到节点的逻辑
//我还尝试了if(low==high),但有时在
//在找到节点的位置
如果(i==下一个| | i<0)
{
返回false;//未找到节点
}
对于(int j=i;j

如果有人能给我举一个排序数组删除算法的好例子,说明一个节点未找到的情况,我会非常感激。我本以为这是一件很容易找到的事情,但我很难找到它。

最简单的答案:如果(I==next | I<0)替换你的
If(I==next | I<0)
带有
if(数据[i].getName().CompareTIgnoreCase(targetListing)!=0)
--换句话说,检查您是否确实找到了匹配项,而不是简单地将范围缩小到一个可能性。

通过使用钢笔和纸张的示例调试或处理函数,应该很容易找到问题。