小编Ane*_*h K的帖子

从数组中删除重复项.打印hashmap时出错

import java.util.*;
public class RemoveDuplicates {

private static Scanner ak;

public static void main(String[] args) {
    ak = new Scanner(System.in);
    int k=0;
    System.out.println("enter the size of the array");
    int n=ak.nextInt();
    int a[]=new int[n];
    for (int i=0;i<n;i++){
        System.out.println("enter element "+(i+1));
        a[i]=ak.nextInt();
    }
    Arrays.toString(a);
    HashMap<Integer,Integer> h=new HashMap<Integer, Integer>();
    for (int i=0;i<n;i++){
        if ((h.containsKey(a[i]))){
            k=h.get(a[i]);
            h.put(a[i],k+1);
        }
        else{
            h.put(a[i], 1);
        }
    }

    System.out.print(h);
    Set <Map.Entry<Integer, Integer>> c=h.entrySet();

    System.out.print(c);
    System.out.println("these are the duplicates removed elements  ");

    Iterator<Map.Entry<Integer, Integer>> i=c.iterator();
    while (i.hasNext()){
        if (i.next().getValue()==1)
        System.out.println(i.next().getKey()); …
Run Code Online (Sandbox Code Playgroud)

java hashmap map set

2
推荐指数
1
解决办法
551
查看次数

标签 统计

hashmap ×1

java ×1

map ×1

set ×1