Java 如何在字符串排序数组中计数等于字符串
我想对我的Java 如何在字符串排序数组中计数等于字符串,java,arrays,string,Java,Arrays,String,我想对我的字符串数组中的相同单词进行计数,并将这些计数打印为整数数组。例如: 输入:String[]s={“be”、“be”、“to”、“on”、“on”、“on”} 产出2,1,3 输入:stringwords[]={“be”、“be”、“not”、“or”、“to”、“to”、“to”、“to”} 产量:2,1,1,3 我的代码: //O(n) public static int [] MaxNumber(String [] arr) { int []
字符串数组中的相同单词进行计数,并将这些计数打印为整数数组。例如:
输入:String[]s={“be”、“be”、“to”、“on”、“on”、“on”}
产出2,1,3
输入:stringwords[]={“be”、“be”、“not”、“or”、“to”、“to”、“to”、“to”}
产量:2,1,1,3
我的代码:
//O(n)
public static int [] MaxNumber(String [] arr)
{
int [] Number_arr=new int[11];
int count=1;
int j=0;
int k=0;
for(int i = 0; i<arr.length-1; i++)
{
if(arr[i].equals(arr[i+1]))
count++;
else{
Number_arr[j]=count;
j++;
count=1;
}
}
return Number_arr;
}
//O(n)
公共静态int[]MaxNumber(字符串[]arr)
{
int[]Number_arr=新int[11];
整数计数=1;
int j=0;
int k=0;
对于(int i=0;i您忘记添加最后一个相等字符串序列的计数:
for(int i = 0; i<arr.length-1; i++)
{
if(arr[i].equals(arr[i+1]))
count++;
else{
Number_arr[j]=count;
j++;
count=1;
}
}
Number_arr[j]=count; // added
for(int i=0;ipublic static int[]MaxNumber(String[]arr)
{
int[]Number_arr=新int[11];
整数计数=1;
int j=0;
int k=0;
对于(int i=0;ipublic static int[]MaxNumber(String[]arr)
{
字符串[]arr={“be”、“be”、“to”、“on”、“on”、“on”};
int[]Number_arr=新int[11];
整数计数=1;
int j=0;
int k=0;
对于(int i=0;i,您给定的输入是:
String [] Sarr= {"be" , "be", "not","not","not", "or","to","to","to"};
您可以看到Sarr[6]
、Sarr[7]
和Sarr[8]
的值是相同的。假设以下值:
i = 6; j = 3; count = 1;
现在调试代码,您将注意到:
i = 7; j = 3; count = 2;
i = 8; j = 3; count = 3;
此时,i=8
测试条件i您得到的答案解决了处理具有连续重复单词的数组的情况。但是,数组中的单词是否总是连续的?如果不是,您可能需要重新考虑您的逻辑
话虽如此,使用来自Java 8的流使这变得相当简单。可能不是O(n),但它完成了任务。我们从数组中获取每个不同的字,然后对它们进行筛选计数
public static void main(String[] args) throws Exception {
String [] Sarr= {"be", "be", "not", "not", "not", "or", "to", "to", "to", "be", "not"};
Arrays.stream(Sarr).distinct()
.forEach(word -> System.out.println(word + ": " + Arrays.stream(Sarr).filter(w -> w.equals(word)).count()));
}
结果:
be: 3
not: 4
or: 1
to: 3
[3, 4, 1, 3]
如果要将计数结果存储到数组中,请尝试以下操作:
public static void main(String[] args) throws Exception {
String [] Sarr= {"be", "be", "not", "not", "not", "or", "to", "to", "to", "be", "not"};
String[] distincts = Arrays.stream(Sarr).distinct().toArray(size -> new String[size]);
long[] wordCount = new long[distincts.length];
for (int i = 0; i < wordCount.length; i++) {
final String distinct = distincts[i];
wordCount[i] = Arrays.stream(Sarr).filter(word -> word.equals(distinct)).count();
}
System.out.println(Arrays.toString(wordCount));
}
另一个使用Java Stream API的简单解决方案是:
Stream<String> stream= Stream.of("a","a","b","c","c");
Map<String, Long> counter = stream.collect(Collectors.groupingBy(String::new, Collectors.counting()));
for (Entry<String, Long> count: counter.entrySet()) {
System.out.println(count.getKey() + " : " + count.getValue());
}
你的代码连续计数等于单词。在数组中不连续地计算重复的单词。这是你喜欢搜索的吗?作为一个例子,考虑数组{“复制”、“其他”、“重复”}。你能再次检查正确的输出吗??2,3之后,你想要打印1或2??
be: 3
not: 4
or: 1
to: 3
public static void main(String[] args) throws Exception {
String [] Sarr= {"be", "be", "not", "not", "not", "or", "to", "to", "to", "be", "not"};
String[] distincts = Arrays.stream(Sarr).distinct().toArray(size -> new String[size]);
long[] wordCount = new long[distincts.length];
for (int i = 0; i < wordCount.length; i++) {
final String distinct = distincts[i];
wordCount[i] = Arrays.stream(Sarr).filter(word -> word.equals(distinct)).count();
}
System.out.println(Arrays.toString(wordCount));
}
[3, 4, 1, 3]
Stream<String> stream= Stream.of("a","a","b","c","c");
Map<String, Long> counter = stream.collect(Collectors.groupingBy(String::new, Collectors.counting()));
for (Entry<String, Long> count: counter.entrySet()) {
System.out.println(count.getKey() + " : " + count.getValue());
}
a : 2
b : 1
c : 2