计算Java中循环迭代的次数
这里我有一个简单的方法,使用hashmaps查找所有重复的数字。这只显示重复的数字,我还需要计算每个数字的出现次数。如何实现for循环来计算每个数字的频率 //司机计算Java中循环迭代的次数,java,Java,这里我有一个简单的方法,使用hashmaps查找所有重复的数字。这只显示重复的数字,我还需要计算每个数字的出现次数。如何实现for循环来计算每个数字的频率 //司机 public class findDuplicates{ public static void main(String[] args){ int [] input = {2,3,6,5,5,6,9,8,7,7,7,4,1,2,5,5,2}; dulicates(input); } public static void
public class findDuplicates{
public static void main(String[] args){
int [] input = {2,3,6,5,5,6,9,8,7,7,7,4,1,2,5,5,2};
dulicates(input);
}
public static void dulicates(int[] MyArray){
Map<Integer,Integer> HashMap = new HashMap<Integer, Integer>();
for (int i : MyArray){
if(!HashMap.containsKey(i)){
HashMap.put(i, 1);
}else{
HashMap.put(i, HashMap.get(i)+1);
}
}
for (Integer i : HashMap.keySet()){
System.out.println("This has a duplicate: " + i );
}
}
}
找到的公共类的副本{
公共静态void main(字符串[]args){
int[]输入={2,3,6,5,5,6,9,8,7,7,4,1,2,5,5,2};
消音(输入);
}
公共静态无效dulicates(int[]MyArray){
Map HashMap=新HashMap();
for(int i:MyArray){
如果(!HashMap.containsKey(i)){
HashMap.put(i,1);
}否则{
HashMap.put(i,HashMap.get(i)+1);
}
}
for(整数i:HashMap.keySet()){
System.out.println(“这有一个副本:+i);
}
}
}
}因为我觉得这可能是一个家庭作业,所以你应该自己做一些计算,所以我将给出基本的想法,而不是代码。这是你自己想出来的 hashmap允许您使用键访问值。因此,对于输入中发现的每个唯一值 检查hashmap中是否有具有该键的元素,如果没有,则在hashmap中放置一个新元素,将找到的值作为其键,将1作为其值 否则,如果hashmap中有一个项具有该键,则将该值拉出,递增并将其放回该键下
通过返回hashmap来结束函数。您已经解决了它,只是错过了迭代并检查值>0
for(Map.Entry<Integer,Integer> kv : map.entrySet())
{
if (kv.getValue()>0)
System.out.println(kv.getKey()+" has "+ kv.getValue() +" duplicate(s)");
}
结果:
有一些
Map
接口的方法可以简化此操作。一个是compute()
- 如果该值为null,则将其初始化为某个值,否则处理现有值
- 上面的一个例子是
,它表示如果compute(i,(k,v)->v==null?1:v+1
键的
为值
,则初始化为null
,否则,将1
添加到现有的1
值中
我闻到作业的味道。你已经在计算发生的次数了。地图上的值是计数。你是如何找到重复的?这应该足够作为一个提示。我很抱歉我一定是故意的,这不是故意的。
static int[] showDuplicatesAndGetCleanArray(int[] myArray) //pls change my name
{
//myArray = {2,3,6,5,5,6,9,8,7,7,7,4,1,2,5,5,2};
Set<Integer> uniques = new HashSet<>(myArray.length);
Map<Integer,Integer> dupMap = new HashMap<>(myArray.length);
for (int i : myArray)
{
if(uniques.contains(i))
dupMap.put(i, dupMap.get(i)==null ? 1 : dupMap.get(i)+1);
else
uniques.add(i); //else not required, I love useless micro-optimizations
}
System.out.println("Total duplicates : [" + (myArray.length-uniques.size())+"]");
for(Map.Entry<Integer,Integer> kv : dupMap.entrySet())
System.out.println("- {" + kv.getKey() + "} has " + kv.getValue() +
" duplicate" + (kv.getValue()>1 ? "s" : "") );
return uniques.stream().mapToInt(Integer::intValue).toArray();
}
Total duplicates : [8]
- {2} has 2 duplicates
- {5} has 3 duplicates
- {6} has 1 duplicate
- {7} has 2 duplicates
/* returned int[] => {1,2,3,4,5,6,7,8,9} */
public static void main(String[] args) {
int [] input = {2,3,6,5,5,6,9,8,7,7,7,4,1,2,5,5,2};
duplicates(input);
}
public static void duplicates(int[] myArray){
Map<Integer,Integer> freq = new HashMap<>();
// perform a frequency count of the array
for (int i : myArray) {
freq.compute(i, (k,v)-> v == null ? 1 : v + 1);
}
// now just print the duplicates and their count.
System.out.println("Duplicates");
freq.forEach((k,v) -> {
if (v > 1) {
System.out.printf("%s occurs %s times.%n", k,v);
}
});
}
Duplicates
2 occurs 3 times.
5 occurs 4 times.
6 occurs 2 times.
7 occurs 3 times.