java.lang.ArrayIndexOutOfBoundsException:索引-1超出二进制搜索长度7的界限

java.lang.ArrayIndexOutOfBoundsException:索引-1超出二进制搜索长度7的界限,java,binary-search,Java,Binary Search,我必须编写一个程序,通过输入“Prezzo”值来搜索“Nomi”的对应项,但当我进行二进制搜索时,我得到了错误: java.lang.ArrayIndexOutOfBoundsException:索引-1超出二进制搜索长度7的界限 代码如下: import java.io.*; public class Blackfriday { public static int ricercaBinaria(Double prezzi[], Double chiave) { int

我必须编写一个程序,通过输入“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);
        }
    }
}
import java.io.*;
公共课黑色星期五{
公共静态内景米尔卡比纳里亚(双前奏[],双交叉){
int-inf=0,sup=prezzi.length-1;

而(inf您的代码至少有两个问题

  • ricercaBinaria
    的先决条件是底层数据数组是有序的。您在测试中使用的是无序数据
  • ricercaBinaria
    将返回给定参数所在的索引(
    Double
    )位于您的数组中。如果未找到它,则将返回
    -1
    。因此,在将其用作索引数组之前,您需要检查返回的索引以验证其确实为正-否则该方法将失败,并出现异常
  • 注意双重平等,但这是另一个问题

  • 下面是一个运行代码:

  • 在异常捕获的情况下始终显示stackTrace,这将有助于调试
  • 如果您管理对象“Double”,并尝试将它们与==”进行比较,它将比较它们的引用(内存中的地址),而不是它们内部包含的值。因此,请使用简单的类型Double或使用equals()方法比较对象
  • 公共课黑色星期五{
    公共静态内景米尔卡比纳里亚(双前奏[],双交叉){
    int-inf=0,sup=prezzi.length-1;
    
    虽然(inf),但我不知道必须订购阵列,非常感谢!!
    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