현재 anagram solver에서 작업 중입니다. 비교하기 전에 사용자 입력과 사전 목록의 글자를 알파벳순으로 정렬하는 것이 좋습니다. 그것은 재미있어 보였으므로 나는 그것을 시험해보고있다. 이전에는 순열 (permutations)을 사용 했었지만, 다중 단어 분석을 해결하기 위해 결국에는 (그리고 효율적으로) 사용할 수있는 무언가를 원합니다.Anagrams의 두 목록 비교 - Java
내 사용자 입력과 사전을 모두 문자 배열로 정렬하고 사전 순으로 정렬 할 수 있습니다. 이제 각각을 비교할 필요가있어서 어떤 것이 아나 그램인지 아닌지를 판단 할 수 있습니다. 알파벳순 사용자 입력을 받고 알파벳순 사전에 포함되어 있는지 여부를 판단하는 방법에 대해 생각했습니다. 아래 코드를 게시했습니다. 당신이 짐작할 수 있듯이 나는이 과정의 논리에 대해 약간 혼란 스럽다. 나는 누군가가 논리를 조금씩 교정 할 수 있는지 궁금해하고 있었다. 어떤 도움을 주셔서 감사합니다.
public class AnagramSolver1 {
public static void main(String[] args) throws IOException {
List<String> dictionary = new ArrayList<String>();
List<String> inputList = new ArrayList<String>();
BufferedReader in = new BufferedReader(new FileReader("src/dictionary.txt"));
String line = null;
Scanner scan = new Scanner(System.in);
while (null!=(line=in.readLine())){
dictionary.add(line);
}
in.close();
char[] sortDictionary;
char[] inputSort;
System.out.println("Enter Word: ");
String input = scan.next();
inputList.add(input);
//Getting a little confused here. I thought about sorting my input
//then iterating through my dictionary (while sorting it too) and comparing
//thus far it produces nothing
for(int i = 0; i < inputList.size(); i++){
inputSort = inputList.get(i).toCharArray();
Arrays.sort(inputSort);
for (int j = 0; j < dictionary.size(); j++) {
sortDictionary = dictionary.get(i).toCharArray();
Arrays.sort(sortDictionary);
if(inputSort.equals(sortDictionary)){
System.out.println("Anagram" +dictionary.get(i));
} //end if
}//end for
}//end for
}//end main
}
좋은 제안처럼 들리지만, 좀 더 생각해 봅시다. – David