Java 对数组中的元素进行计数
我制作了一个计算数组中元素的程序。它可以工作,但我的程序中有一种错误 我希望我的程序的输出如下:Java 对数组中的元素进行计数,java,arrays,count,Java,Arrays,Count,我制作了一个计算数组中元素的程序。它可以工作,但我的程序中有一种错误 我希望我的程序的输出如下: 发生1次:2次 发生2次:1次 发生3次:1次 发生6次:1次 String[] values= {"1", "1", "3", "6", "2"}; int[] counts = new int[values.length]; Arrays.sort(values); int temp = 0; int c = 0; for(int i = 0; i < values.length; i+
发生1次:2次
发生2次:1次
发生3次:1次
发生6次:1次
String[] values= {"1", "1", "3", "6", "2"};
int[] counts = new int[values.length];
Arrays.sort(values);
int temp = 0;
int c = 0;
for(int i = 0; i < values.length; i++){
counts[i] = Integer.parseInt(values[i]);
for(int j = 0;j < counts.length; j++) {
if(counts[i] == counts[j]) {
c++;
}
}
System.out.println(counts[i] + " occured: " + c +" times");
c = 0;
}
但我的程序给出了这样一个输出:发生1次:1次
发生1次:2次
发生2次:1次
发生3次:1次
发生6次:1次
String[] values= {"1", "1", "3", "6", "2"};
int[] counts = new int[values.length];
Arrays.sort(values);
int temp = 0;
int c = 0;
for(int i = 0; i < values.length; i++){
counts[i] = Integer.parseInt(values[i]);
for(int j = 0;j < counts.length; j++) {
if(counts[i] == counts[j]) {
c++;
}
}
System.out.println(counts[i] + " occured: " + c +" times");
c = 0;
}
String[]值={“1”、“1”、“3”、“6”、“2”};
int[]计数=新的int[values.length];
数组。排序(值);
内部温度=0;
int c=0;
对于(int i=0;i问题是:您只希望出现四条打印语句,但得到五条。因为这段代码缺少大括号并且缩进不好,所以您可能会看到println函数属于或不属于i循环。i循环将运行多少次?提示:它是i.length,在本例中等于_u(您填写空格)
一旦你明白为什么会有一个额外的println,试着修复你的代码,然后如果你需要帮助,带着特定的问题回来。问题是:你只希望出现四个print语句,但得到五个。因为这段代码缺少大括号并且缩进不好,所以您可能会看到println函数属于或不属于i循环。i循环将运行多少次?提示:它是i.length,在本例中等于_u(您填写空格)
一旦你明白了为什么会有一个额外的println,试着修复你的代码,然后如果你需要帮助,带着特定的问题回来。看,与你的方法类似,但只使用一个数组(并且没有hashmaps)。我测试过了,它能工作
String[] values= {"1","1","3","6","2"};
Arrays.sort(values);
int c=1,i=0;
while(i<values.length-1){
while(values[i].equals(values[i+1])){
c++;
i++;
}
System.out.println(values[i] + " appeared " + c + " times");
c=1;
i++;
if(i==values.length-1)
System.out.println(values[i] + " appeared " + c + " times");
}
String[]值={“1”、“1”、“3”、“6”、“2”};
数组。排序(值);
int c=1,i=0;
虽然(我看,与您的方法类似,但只使用一个数组(并且没有hashmaps)。我测试过,它可以工作
String[] values= {"1","1","3","6","2"};
Arrays.sort(values);
int c=1,i=0;
while(i<values.length-1){
while(values[i].equals(values[i+1])){
c++;
i++;
}
System.out.println(values[i] + " appeared " + c + " times");
c=1;
i++;
if(i==values.length-1)
System.out.println(values[i] + " appeared " + c + " times");
}
String[]值={“1”、“1”、“3”、“6”、“2”};
数组。排序(值);
int c=1,i=0;
虽然(i您的代码在打印其决定时速度太快:您需要为值的每个项目调用一次println
,而不是为值数组中的每个不同项目调用一次
一种方法是使用Map
对项目进行计数。您可以这样做:
Map<String,Integer> counts = new HashMap<String,Integer>();
for (String s : values) {
if (counts.containsKey(s)) {
int old = counts.get(s);
counts.put(s, old+1);
} else {
counts.put(s, 1);
}
}
for (Map.Entry<String,Integer> entry : counts.entrySet()) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}
Map counts=newhashmap();
用于(字符串s:值){
如果(包括集装箱){
int old=counts.get(s);
计数。放置(s,旧+1);
}否则{
计数。放置(s,1);
}
}
对于(Map.Entry:counts.entrySet()){
System.out.println(entry.getKey()+“-”+entry.getValue());
}
.您的代码在打印其决定时速度太快:您需要为值
数组中找到的每个不同项调用一次,而不是为每个值
项生成一个println
一种方法是使用Map
对项目进行计数。您可以这样做:
Map<String,Integer> counts = new HashMap<String,Integer>();
for (String s : values) {
if (counts.containsKey(s)) {
int old = counts.get(s);
counts.put(s, old+1);
} else {
counts.put(s, 1);
}
}
for (Map.Entry<String,Integer> entry : counts.entrySet()) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}
Map counts=newhashmap();
用于(字符串s:值){
如果(包括集装箱){
int old=counts.get(s);
计数。放置(s,旧+1);
}否则{
计数。放置(s,1);
}
}
对于(Map.Entry:counts.entrySet()){
System.out.println(entry.getKey()+“-”+entry.getValue());
}
.Apache有一个内置的实用程序方法,类似于dasblinkenlight的方法:
Map<String, Integer> counts =
CollectionsUtils.getCardinalityMap(Arrays.asList(values));
for (Map.MapEntry<String,Integer> entry : counts) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}
Map计数=
getCardinalityMap(Arrays.asList(values));
对于(Map.MapEntry:计数){
System.out.println(entry.getKey()+“-”+entry.getValue());
}
编辑:
更新旧答案。Java 8流有一个内置的等价物:
Map<Stirng, Long> =
Arrays.stream(values)
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()));
Map=
Arrays.stream(值)
.collect(收集器.groupingBy(Function.identity()),
收集器。计数();
Apache的内置实用程序方法与dasblinkenlight的方法类似:
Map<String, Integer> counts =
CollectionsUtils.getCardinalityMap(Arrays.asList(values));
for (Map.MapEntry<String,Integer> entry : counts) {
System.out.println(entry.getKey() + " - " + entry.getValue());
}
Map计数=
getCardinalityMap(Arrays.asList(values));
对于(Map.MapEntry:计数){
System.out.println(entry.getKey()+“-”+entry.getValue());
}
编辑:
更新旧答案。Java 8流有一个内置的等价物:
Map<Stirng, Long> =
Arrays.stream(values)
.collect(Collectors.groupingBy(Function.identity(),
Collectors.counting()));
Map=
Arrays.stream(值)
.collect(收集器.groupingBy(Function.identity()),
收集器。计数();
你可以用hashmap来做这件事。看看:)请避免使用没有花括号的statemt。你可以用hashmap来做这件事。看看:)请避免使用没有花括号的statemt。这真是太可怕了:value[i]==value[i+1]
之所以有效,是因为编译器实习生字符串文字。尝试从用户输入中读取值
,以查看此片段。强制打印循环中最后一项的逻辑是如此复杂,以至于我的大脑受伤。我知道你把OP的方法作为你的出发点,所以我不会对此投反对票,但我认为这样的修正可能会教给OP一个完全错误的观点。这真的很糟糕:values[I]==values[I+1]
甚至有效的原因是编译器实习生字符串。尝试从用户输入中读取值
,以查看此片段。强制打印循环中最后一项的逻辑是如此复杂,以至于我的大脑受伤。我知道你把OP的方法作为你的出发点,所以我不会投反对票,但我认为这样的修正可能会教OP一个完全错误的观点。