使用并行 Java 数组计算单词
Counting words using parallel Java arrays
我正在尝试编写代码来计算数组中每个单词的出现频率。我想知道是否需要使用两个嵌套循环来跟踪包含整数的数组和包含单词的数组。我已经摆弄了几个小时的代码,但无法取得任何进展。如果您有任何指示,将不胜感激。我得到的输入首先是数组中元素的数量,然后是我应该计算其频率的单词,例如 4 dog cat dog fish。
import java.util.Scanner;
public class LabProgram {
public static int getFrequencyOfWord(String[] wordsList, int listSize, String currWord) {
int i;
int count;
int[] countArr = new int[listSize];
// need to use two arrays; one for frequency, one for words.
for (i = 0; i < countArr.length; ++i) {
if (wordsList[i].compareTo(currWord) == 0) {
countArr[i] += 1;
}
}
//check previous LAB that had same concept; then make it use a method.
return countArr[i];
}
public static void main(String[] args) {
int size;
int i;
size = scnr.nextInt();
String[] array = scnr.nextLine().split(" ");
for (i = 0; i < array.length; ++i) {
System.out.println(array[i]);
}
for (i = 0; i < array.length; ++i) {
currWord = array[i];
System.out.println(currWord + getFrequencyOfWord(array, size, currWord));
}
}
}
你能试试下面的解决方案吗?:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
try (Scanner scnr = new Scanner(System.in)) {
int size = scnr.nextInt();
Map<String, Integer> wordCounts = new HashMap<>();
for (int i = 0; i < size; i++) {
String s = scnr.next();
if (wordCounts.containsKey(s)) {
int count = wordCounts.get(s);
wordCounts.put(s, ++count);
} else {
wordCounts.put(s, 1);
}
}
wordCounts.entrySet().stream().forEach(s -> System.out.println(
s.getKey() + ": " + s.getValue()));
}
}
}
使用 Java 8 个流:
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class LabProgram {
public static void main(String[] args) {
try (Scanner scnr = new Scanner(System.in)) {
int size = scnr.nextInt();
String array[] = new String[size];
for (int i = 0; i < size; i++) {
array[i] = scnr.next();
}
Map<Object, Integer> data = Stream.of(array)
.collect(Collectors.groupingBy(
s -> s, Collectors.summingInt(s -> 1)));
data.entrySet().stream().forEach(s -> System.out.println(
s.getKey() + ": " + s.getValue()));
}
}
}
如果您的输入是:
4 dog cat dog fish
输出:
cat: 1
fish: 1
dog: 2
我正在尝试编写代码来计算数组中每个单词的出现频率。我想知道是否需要使用两个嵌套循环来跟踪包含整数的数组和包含单词的数组。我已经摆弄了几个小时的代码,但无法取得任何进展。如果您有任何指示,将不胜感激。我得到的输入首先是数组中元素的数量,然后是我应该计算其频率的单词,例如 4 dog cat dog fish。
import java.util.Scanner;
public class LabProgram {
public static int getFrequencyOfWord(String[] wordsList, int listSize, String currWord) {
int i;
int count;
int[] countArr = new int[listSize];
// need to use two arrays; one for frequency, one for words.
for (i = 0; i < countArr.length; ++i) {
if (wordsList[i].compareTo(currWord) == 0) {
countArr[i] += 1;
}
}
//check previous LAB that had same concept; then make it use a method.
return countArr[i];
}
public static void main(String[] args) {
int size;
int i;
size = scnr.nextInt();
String[] array = scnr.nextLine().split(" ");
for (i = 0; i < array.length; ++i) {
System.out.println(array[i]);
}
for (i = 0; i < array.length; ++i) {
currWord = array[i];
System.out.println(currWord + getFrequencyOfWord(array, size, currWord));
}
}
}
你能试试下面的解决方案吗?:
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class LabProgram {
public static void main(String[] args) {
try (Scanner scnr = new Scanner(System.in)) {
int size = scnr.nextInt();
Map<String, Integer> wordCounts = new HashMap<>();
for (int i = 0; i < size; i++) {
String s = scnr.next();
if (wordCounts.containsKey(s)) {
int count = wordCounts.get(s);
wordCounts.put(s, ++count);
} else {
wordCounts.put(s, 1);
}
}
wordCounts.entrySet().stream().forEach(s -> System.out.println(
s.getKey() + ": " + s.getValue()));
}
}
}
使用 Java 8 个流:
import java.util.Map;
import java.util.Scanner;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class LabProgram {
public static void main(String[] args) {
try (Scanner scnr = new Scanner(System.in)) {
int size = scnr.nextInt();
String array[] = new String[size];
for (int i = 0; i < size; i++) {
array[i] = scnr.next();
}
Map<Object, Integer> data = Stream.of(array)
.collect(Collectors.groupingBy(
s -> s, Collectors.summingInt(s -> 1)));
data.entrySet().stream().forEach(s -> System.out.println(
s.getKey() + ": " + s.getValue()));
}
}
}
如果您的输入是:
4 dog cat dog fish
输出:
cat: 1
fish: 1
dog: 2