java数组搜索方法

时间:2015-04-03 17:01:13

标签: java arrays overloading

我必须制作一个数组,其中10个数字由用户通过扫描仪选择。程序按升序排序10个数字,然后打印出新的列表。然后它要求用户输入任何数字,然后使用二进制搜索来查看该数字是否在10个数字的列表中。

这是我到目前为止所拥有的:

import java.util.Scanner;

public class lab11 {

    public static void main(String[] args){
        double[] numbers = new double[10];        
        System.out.println("Please enter 10 double values:");
        for (int i=0;i<10;i++){
            numbers[i] = inputArray();
        }
        System.out.println("sorting");
        print(selectionSort(numbers));

        System.out.println("Please enter a search key:");

    }
    public static double inputArray(){

        Scanner input = new Scanner(System.in);
        System.out.print(">");
        double d = input.nextInt();
        return d;
    }

    public static double[] selectionSort(double[] list){
        double temp;
        for(int i=0; i < (list.length-1); i++){
            for(int j = i+1; j < list.length; j++){
                if(list[j] < list[i]){
                    temp = list[i];
                    list[i] = list[j];
                    list[j] = temp;
                }
            }
        }
        return list;
    }
    public static void print(double[] arr){
        for(double d:arr){

            System.out.println("list["+j+"]"+" = "+d);

        }

    }
    public static int binarySearch(double[]list, double key){

        int low=0,high=list.length -1;

        key =input.nextInt();

        while(high>=low){
            int mid=(low+high)/2;
            if(key<list[mid])
                    high=mid-1;
            else if(key==list[mid]) return mid;
            else low=mid+1;
        }
        return-1;
        }

    }

我需要2件事的帮助。

(1)属于Print方法。我希望程序打印出像&#34; list [i] = d&#34; d是用户输入的数字并且工作正常,但我不知道。我希望它是0到9的数组编号。

(2)我需要帮助调用二进制搜索,这样我就可以获得搜索输出。

3 个答案:

答案 0 :(得分:0)

我会这样写

public static void main(String[] args) {

    // initialize
    double[] numbers = new double[10];
    Scanner input = new Scanner(System.in);

    // insert variables into array
    inputArray(numbers, input);

    // start sorting
    System.out.println("sorting");
    selectionSort(numbers);

    // print the sorted array
    print(numbers);

    // binary search
    binarySearch(numbers, input);

    // close scanner
    input.close();
}

public static void inputArray(double[] numbers, Scanner input) {
    System.out.println("Please enter 10 double values:");
    for (int i = 0; i < 10; i++) {
        System.out.print(">");
        numbers[i] = input.nextDouble();
    }
}

public static void print(double[] numbers) {
    for (int i = 0; i < numbers.length; i++) {
        System.out.println("list[" + i + "]" + " = " + numbers[i]);
    }
}

public static double[] selectionSort(double[] list) {
    double temp;
    for (int i = 0; i < (list.length - 1); i++) {
        for (int j = i + 1; j < list.length; j++) {
            if (list[j] < list[i]) {
                temp = list[i];
                list[i] = list[j];
                list[j] = temp;
            }
        }
    }
    return list;
}

public static void binarySearch(double[] numbers, Scanner input) {
    System.out.println("Please enter a search key:");
    double key = input.nextDouble();
    int index = Arrays.binarySearch(numbers, key);
    if (index < 0) {
        System.out.println(key + " is not in the list");
    } else {
        System.out.println(key + " is in the list, index = " + index);
    }
}

答案 1 :(得分:0)

只是为了正确的输入,你可以编写一个do ... while循环,循环直到输入有效:

for (int i = 0; i < 10; i++) {
        double number;
        Scanner input = new Scanner(System.in);
        do {
            System.out.print(">");
            number = input.nextDouble();
        } while (number < 0 || number > 10);
    }
}

答案 2 :(得分:0)

对于您似乎想要使用的输出

// [...] perform the sorting [...]

// enter the search key
System.out.println("Please enter a search key:");
final int position = Arrays.binarySearch(numbers, input.nextDouble());
if (position < 0) {
    System.out.println("key is not in the list");
} else {
    System.out.println("key is at position " + position);
}