java二进制搜索从数组列表,词典单词

时间:2014-11-05 18:36:00

标签: java

我的代码读取一个名为sort.txt的文件,其中有按字母顺序和长度排序的词典单词。每行中有一个单词。该程序工作正常,请不要评论它是如何写的。然后,用户输入他正在搜索的单词,例如, " C **",程序返回所有可能的匹配(Car,Cat,Cam等)。我的问题是如何使用二进制搜索来搜索数组以加快速度。但二进制搜索仅在用户输入第一个或前两个或前3个字符时使用,例如" Ca *"或者" Mou **"。如果用户输入" *** se"例如,然后程序将跳过二进制搜索并搜索整个数组。

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;

public class main{

public static void main(String[] args) {
    String izbira;
    int dolzina=0;
    Scanner in = new Scanner(System.in);
    String vnos;
    Scanner input = new Scanner(System.in);

    ArrayList list1 = new ArrayList();
    ArrayList list2 = new ArrayList();
    ArrayList list3 = new ArrayList();
    ArrayList list4 = new ArrayList();
    ArrayList list5 = new ArrayList();
    ArrayList list6 = new ArrayList();
    ArrayList list7 = new ArrayList();
    ArrayList list8 = new ArrayList();
    ArrayList list9 = new ArrayList();
    ArrayList list10plus = new ArrayList();

    try {

        File file = new File("sort.txt");
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferedReader = new BufferedReader(fileReader);
        String vrstica;

        while ((vrstica = bufferedReader.readLine()) != null) {
            if (vrstica.length() == 1) {
                list1.add(vrstica);
            }
            if (vrstica.length() == 2) {
                list2.add(vrstica);
            }
            if (vrstica.length() == 3) {
                list3.add(vrstica);
            }
            if (vrstica.length() == 4) {
                list4.add(vrstica);
            }
            if (vrstica.length() == 5) {
                list5.add(vrstica);
            }
            if (vrstica.length() == 6) {
                list6.add(vrstica);
            }
            if (vrstica.length() == 7) {
                list7.add(vrstica);
            }
            if (vrstica.length() == 8) {
                list8.add(vrstica);
            }
            if (vrstica.length() == 9) {
                list9.add(vrstica);
            }
            if (vrstica.length() > 9) {
                list10plus.add(vrstica);
            }
        }
        do{
            do {
                System.out.println("Vnesi dožino besede, ki jo iščeš:");
                if (in.hasNextInt()) {
                    dolzina = in.nextInt();
                } else if (in.hasNextLine()) {
                    System.out.printf("Napačen vnos! Poskusi ponovno:%n ",
                            in.nextLine());
                } 
            } while (dolzina <= 0);



        System.out.println("Vnesi besedo za neznano črko vpiši * :");
        vnos = input.nextLine();
        vnos = vnos.replace("*", ".");

        if (dolzina == 1) {
            for (int i = 0; i < list1.size(); i++) {
                String s = (String) list1.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }

        }

        if (dolzina == 2) {
            for (int i = 0; i < list2.size(); i++) {
                String s = (String) list2.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }

        }
        if (dolzina == 3) {

            for (int i = 0; i < list3.size(); i++) {
                String s = (String) list3.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }
        }
        if (dolzina == 4) {

            for (int i = 0; i < list4.size(); i++) {
                String s = (String) list4.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }
        }
        if (dolzina == 5) {
            for (int i = 0; i < list5.size(); i++) {
                String s = (String) list5.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }
        }
        if (dolzina == 6) {
            for (int i = 0; i < list6.size(); i++) {
                String s = (String) list6.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }
        }
        if (dolzina == 7) {
            for (int i = 0; i < list7.size(); i++) {
                String s = (String) list7.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }
        }
        if (dolzina == 8) {
            for (int i = 0; i < list8.size(); i++) {
                String s = (String) list8.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }
        }
        if (dolzina == 9) {
            for (int i = 0; i < list9.size(); i++) {
                String s = (String) list9.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }
        }
        if (dolzina > 9) {
            for (int i = 0; i < list10plus.size(); i++) {
                String s = (String) list10plus.get(i);
                if (s.matches(vnos))
                    System.out.println(s);
            }

        }
        dolzina=-1;
        System.out.println("Ponovni vnos (da/ne):");
        Scanner inn= new Scanner (System.in);
        izbira = inn.next();

    }while (izbira.equalsIgnoreCase("da"));
        bufferedReader.close();
    } catch (IOException e) {
        e.printStackTrace();

    }
}}

1 个答案:

答案 0 :(得分:1)

这不会给你一个完整的答案,但只是放在那个方向。

您需要检查第一个字符是否不是*,然后执行二进制搜索,否则迭代所有字符串并执行String.endsWith()

if(vnos.charAt(0) != '*') { //do binary search with the substring } else { //iterate and check if the string endsWith given suffix. }