Java:如何使用数组进行二进制搜索

时间:2017-10-02 17:19:08

标签: java arrays binary-search

对于这个java程序,我试图对我在类号中创建的数组使用二进制搜索,但是,当我进入第4个选项时,程序就结束了。 binSearch方法属于Class Numbers。我可以输入所需的数组大小,生成随机数,搜索特定数字,然后显示它们。但是,当我想使用二进制搜索时,程序如前所述结束。程序结束的原因是什么,以及需要采取哪些措施来修复某种方法?

public class Numbers {

    int[] array;
    private int sizeOfArray;

    public Numbers() {
        sizeOfArray = 0;
        array= new int [sizeOfArray];
    } 

    public Numbers(int sizeOfArray) {
        this.sizeOfArray = sizeOfArray;
        array= new int [sizeOfArray];
    } 


    public void generateNumbers() {
        Random randomNumber = new Random();
        int theNumber = 0;
        for (int i = 0; i < sizeOfArray; i++) {
            theNumber = randomNumber.nextInt(50);
            array[i] = theNumber;
        }
    } 

    public int count(int num) {
        int theNumbers = 0;
        for (int i = 0; i < sizeOfArray; i++) {
            if (array[i] == num) {
                theNumbers++;
            }
        }
        return theNumbers;
    } // end count method

    public String toString() {
        String myArray = "";
        for (int i = 0; i < sizeOfArray; i++) {
            myArray += array[i] + " ";
        }
        return myArray;
    } 

    public int binSearch(int[] array, int key) {
        int low = 0;
        int high = sizeOfArray - 1;
        //int middle = (low + high + 1) /2;
        int location = -1;

        while (high >= low) {
            int middle1 = (low + high) / 2;
            if (array[middle1] == key ) {
                //return true;
            }
            if (array[middle1] < key) {
                low = middle1 + 1;
            }
            if (array[middle1] > key) {
                high = middle1 - 1;
            }
        }
            //return false;
        return location;
    }
}

这是主菜单:

    boolean isDone = false;
    String input = null;
    Numbers theNumber = new Numbers();
    Scanner scanner = new Scanner (System.in);

    try {

        while (isDone == false) {
            /*Menu options */
            System.out.println("Enter 1 to create array size");
            System.out.println("Enter 2 to generate random numbers");
            System.out.println("Enter 3 to search and display number of occurrences");
            System.out.println("Enter 4 to binary search to find whether specific number exists");
            System.out.println("Enter 5 to display the array");
            System.out.println("Enter 6 to quit the program");

            input = scanner.nextLine(); 

            switch (input) {
            case "1":
                int intNumber1 = 0;
                System.out.println("Enter required size:");
                intNumber1 = Integer.valueOf(scanner.nextLine());
                theNumber = new Numbers(intNumber1);
                System.out.println("Array has been generated.");
                break;
            case "2":
                //theNumber = new Numbers();
                theNumber.generateNumbers();
                System.out.println("Numbers have been generated and stored.");
                break;
            case "3":
                int intNumber2 = 0;
                System.out.println("Enter number to search for: ");
                intNumber2 = Integer.valueOf(scanner.nextLine());
                System.out.println("Number of occurences of " + intNumber2 + " in the array is " + theNumber.count(intNumber2) + ".");
                break;
            case "4":
                int key = 0;
                theNumber.binSearch(null, key);
                System.out.println("Array is sorted: ");
                break;
            case "5":
                int theNumbers = 0;
                if (theNumbers == 0)
                {
                    System.out.println("No array has not been generated yet.");
                }
                else 
                {
                    System.out.println("The numbers are: ");
                }

                System.out.println(theNumber.toString());
                break;
            case "6":
                isDone = true;

                System.out.println("Bye... See you again");
                scanner.close();
                break;
            default:
                System.out.println("These are invalid choices...please reenter.");
                break;
            } 

        } 
    }
    catch (Exception exception) 
    {
        System.out.println("This is an invalid choice...please reenter.");
        scanner.nextLine();
        return; 
    } 

0 个答案:

没有答案