모든 댓글이 달렸으나 코드로 변환하려고합니다. 각 단계에서 수정되지 않은 모든 줄을 주석 처리하여 변경 사항이 명확 해졌습니다.
먼저 2 차원 어레이를 나사로 고정합니다. 제한적이고 다루기가 힘듭니다. 의 대신지도를 사용하자 :
public static Map<String, Integer> sortWords(BufferedReader in) throws IOException{
// String line = "";
Map<String, Integer> wordsCount = new HashMap<>();
//
// while((line=in.readLine())!=null) {
// String a[]=line.split(" ");
// for(int i = 0; i < a.length; i++) {
// a[i] = a[i].toUpperCase();
Integer count = wordsCount.get(a[i]); // Get current count for this word
if (count == null) count = 0; // Initialize on first appearance
count++; // Update counter
wordsCount.put(a[i], count); // Save the updated value
// }
// line=in.readLine();
// }
// return words;
//}
그냥 그 단어에 연관된 값을 얻을하고 업데이트 ... 배열, 추가 루프, 아니 String
int
로 변환을 초기화 할 필요. 이제는 사전에 단어의 개수를 알 필요가 없으므로 두 번째 int n
매개 변수를 안전하게 제거 할 수 있습니다!
자, 이제 2000 년 이전의 매우 숙달 된 C- 같은 이디엄을 사용하고있는 것을 볼 수 있습니다 (모든 for(;;)
및 배열 포함). 그것은 완벽하게 유효하지만 더 현대적이고 유용한 구조를 놓치고 있습니다. 따라서 우리는 2004 년 이래로 enhanced for loop을 사용할 수 있습니까?
//public static Map<String, Integer> sortWords(BufferedReader in) throws IOException{
// String line = "";
// Map<String, Integer> wordsCount = new HashMap<>();
//
// while((line=in.readLine())!=null) {
// String a[]=line.split(" ");
for(String word : a) {
word = word.toUpperCase();
Integer count = wordsCount.get(word); // Get current count for this word
// if (count == null) count = 0; // Initialize on first appearance
// count++; // Update counter
wordsCount.put(word, count); // Save the updated value
// }
// line=in.readLine();
// }
// return wordsCount;
//}
선명하게 구문은, 우리는 우리가 루프에서 다루고있는 개체의 유형을 정확히 알고 ... 그리고 무엇보다도, 그것은 청소기 만들기 위해 당신에게 인라인 코드의 일부를 할 수 있습니다. 이제 toUpperCase()
방법 대신 단어 당 한번의 라인 당 한 번만 호출됩니다, 우리는
마지막 것은 ;-P 왼쪽 모두의 눈을 아프게 한 그 String a[]
제거있어
//public static Map<String, Integer> sortWords(BufferedReader in) throws IOException{
// String line = "";
// Map<String, Integer> wordsCount = new HashMap<>();
//
// while((line=in.readLine())!=null) {
for(String word : line.toUpperCase().split(" ")) {
// Integer count = wordsCount.get(word); // Get current count for this word
// if (count == null) count = 0; // Initialize on first appearance
// count++; // Update counter
// wordsCount.put(word, count); // Save the updated value
// }
// line=in.readLine();
// }
// return wordsCount;
//}
을 : 이것처럼 해야할 일은 그 끝에 여분의 readLine()
을 제거하는 것입니다. 그렇게하면 코드가 다음과 같이 표시됩니다.
public static Map<String, Integer> sortWords(BufferedReader in) throws IOException {
String line = "";
Map<String, Integer> wordsCount = new HashMap<>();
while ((line = in.readLine()) != null) {
for(String word : line.toUpperCase().split(" ")) {
Integer count = wordsCount.get(word); // Get current count for this word
if (count == null) count = 0; // Initialize on first appearance
count++; // Update counter
wordsCount.put(word, count); // Save the updated value
}
}
return wordsCount;
}
훨씬 좋네요!
이 같은 방법을 사용할 수 있습니다
BufferedReader in = new BufferedReader(new FileReader("myWords.txt"));
Map words = sortWords(in);
int numberOfHellos = words.get("Hello");
int numberOfGreetings = numberOfHellos + words.get("Hi") + words.get("Howdy");
이 코드`}}} 라인 = in.readLine()로 줄을 건너 뛰는 것
,' –
은 1부터 시작하는 카운트를 같이, 때 카운터를 갱신 한 단어의 첫 번째 발생을 발견하고 1 회 발생 및 count == 2.로 종료하십시오. 0에서 시작하거나 루프에서 초기화 부분을 이동해야합니다. 그리고 배열의 길이를 줄수로 설정했지만 찾을 수있는 단어는 그 이상입니다. –
단어와 그 어커런스를'HashMap'에 저장하는 것이 더 쉽습니다. 새로운 단어를 찾을 때마다'HashMap'에 넣고 값을'1'에 넣고 이미 해시에 있다면'++ '또는 원하는 것을 사용하여 값을 업데이트합니다. –
Justplayit94