내 코드는 알파벳순으로 또는 길이순으로 어순 단어가있는 sort.txt
이라는 파일을 읽습니다. 각 줄에는 한 단어가 있습니다. 이 프로그램은 잘 작동하며 작성 방법에 대해서는 언급하지 마십시오. 사용자는 그가 검색하고있는 단어를 입력한다. "C **", 프로그램은 가능한 모든 일치 항목 (Car, Cat, Cam 등)을 반환합니다. 내 질문은 것들을 속도를 이진 검색을 사용하여 배열을 검색하는 방법입니다. 그러나 첫 번째 또는 첫 번째 또는 두 번째 또는 세 번째 문자가 사용자에 의해 입력 된 경우에만 이진 검색이 사용됩니다 (예 : "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();
}
}}
** 많은 코드 **입니다. [mcve] 얻을 수있는 기회 (http://stackoverflow.com/help/mcve)? – Mureinik
[String.startWith()] 사용 (http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#startsWith (java.lang.String)) – StackFlowed
그냥 반복됩니다. 암호. 똑같은 코드를 10 번 사용하면 빨리 볼 수 있습니다. –