1超出二进制搜索中长度7的界限

7tofc5zh  于 2021-07-08  发布在  Java
关注(0)|答案(2)|浏览(463)

我必须编写一个程序,通过输入“prezzo”值来搜索“nomi”的对应项,但是当我进行二进制搜索时,我得到了错误:
java.lang.arrayindexoutofboundsexception:索引-1超出二进制搜索中长度7的界限
代码如下:

import java.io.*;

public class Blackfriday {
    public static int ricercaBinaria(Double prezzi[], Double chiave) {
        int inf = 0, sup = prezzi.length - 1;
        while (inf <= sup) {
            int med = (inf + sup) / 2;
            if (prezzi[med] == chiave)
                return med;
            if (prezzi[med] < chiave)
                inf = med + 1;
            else
                sup = med - 1;
        }
        return -1;
    }

    public static void main(String args[]) {
        try {
            InputStreamReader isr;
            BufferedReader br;
            double cerca;
            isr = new InputStreamReader(System.in);// abilitato la lettura da tastiera
            br = new BufferedReader(isr);// abilitato la lettura un rigo per volta
            String nomi[] = { "picones", "vinile di Speranza", "Laurea", "King Mufasa", "Pentium Gold",
                    "Aethey Wind breaker ORO", "HeelCompletoSpaic1we" };
            Double prezzi[] = { 2.0, 13.50, 23.0, 99.50, 120.0, 75.20, 999.99 };
            System.out.println("Quanto vuoi spendere?");
            String xStringa = br.readLine();// ricevo la digitazione in String
            cerca = Double.parseDouble(xStringa);// Trasformo la String in double
            System.out.println("Puoi comprare: " + nomi[ricercaBinaria(prezzi, cerca)]);

        } catch (Exception e) {
            System.out.println(e);
        }
    }
}
btqmn9zl

btqmn9zl1#

下面是一个运行代码:
在异常捕获的情况下总是显示stacktrace,这将有助于调试
如果您“double”管理对象并尝试将它们与==进行比较,它将比较它们的引用(内存中的地址),而不是其中包含的值。所以使用简单类型double或compare objects with equals()方法。

public class Blackfriday {
    public static int ricercaBinaria(double prezzi[], Double chiave) {
        int inf = 0, sup = prezzi.length - 1;
        while (inf <= sup) {
            int med = (inf + sup) / 2;
            if (prezzi[med] == chiave)
                return med;
            if (prezzi[med] < chiave)
                inf = med + 1;
            else
                sup = med - 1;
        }
        return -1;
    }

    public static void main(String args[]) {
        try {
            InputStreamReader isr;
            BufferedReader br;
            double cerca;
            isr = new InputStreamReader(System.in);//abilitato la lettura da tastiera
            br = new BufferedReader(isr);//abilitato la lettura un rigo per volta
            String nomi[] = {"picones", "vinile di Speranza", "Laurea", "King Mufasa", "Pentium Gold", "Aethey Wind breaker ORO", "HeelCompletoSpaic1we"};
            double prezzi[] = {2.0, 13.50, 23.0, 99.50, 120.0, 75.20, 999.99};
            System.out.println("Quanto vuoi spendere?");
            String xStringa = br.readLine();//ricevo la digitazione in String
            cerca = Double.parseDouble(xStringa);//Trasformo la String in double
            System.out.println("Puoi comprare: " + nomi[ricercaBinaria(prezzi, cerca)]);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }//main
}//classe
5cnsuln7

5cnsuln72#

你的代码至少有两个问题。
先决条件 ricercaBinaria 基础数据数组是有序的。您在测试中使用的是无序数据 ricercaBinaria 将返回给定参数所在的索引( Double )位于您的阵列中。如果没有找到,那么 -1 将被退回。因此,在将其用作索引数组之前,您需要检查返回的索引以验证它是否确实是正的,否则该方法将失败,并出现您看到的异常
小心双重平等,但这是另一个问题。

相关问题