简体   繁体   English

为什么列表中的最后一个元素显示为 null

[英]why does the last element in list appear as null

The code:代码:

public class main {
public static void countFrequencies(ArrayList<String> list,int n)
{
    int k=0;
    Map<String, Integer> hm = new HashMap<String, Integer>();

    for (String i : list) {
        Integer j = hm.get(i);
        hm.put(i, (j == null) ? 1 : j + 1);
    }

    for (Map.Entry<String, Integer> val : hm.entrySet()) {
        System.out.println("Element " + val.getKey() + " "
                + "occurs"
                + ": " + val.getValue() + " times");
        k++;
        if(k==n)
            break;
    }
}

public static void main(String[] args)  {
    String nr1;
    ArrayList<String> nr = new ArrayList<String>();
    Scanner obj= new Scanner(System.in);
    System.out.println("Input n");
    int n= obj.nextInt();

    System.out.println("Input k ");
    int k= obj.nextInt();
    System.out.println("Input n elemente: ");
    for(int i=1;i<=n;i++)
    {
            nr1=obj.nextLine();
            System.out.println("Element " + (i));
            nr.add(nr1);
    }
    countFrequencies(nr,k);


}

} }

If I run the code everything goes ok until I input the last element in the list, if do It appears as null, it simply doesn't let me input it, also when I print the frequencies list I need it to show the top k elements after the number of frequencies, in the descending order.如果我运行代码,一切正常,直到我输入列表中的最后一个元素,如果它显示为 null,它根本不允许我输入它,而且当我打印频率列表时,我需要它来显示前 k频率数之后的元素,按降序排列。

   public static void countFrequencies(ArrayList<String> list,int n)
{
    int k=0;
    Map<String, Integer> hm = new HashMap<String, Integer>();
    Map<String, Integer> hm2 = new LinkedHashMap<>();

    for (String i : list) {
        Integer j = hm.get(i);
        hm.put(i, (j == null) ? 1 : j + 1);
    }

    hm.entrySet().stream()
            .sorted(Map.Entry.comparingByValue(Comparator.reverseOrder()))
            .forEachOrdered(i -> hm2.put(i.getKey(),i.getValue()));

    for (Map.Entry<String, Integer> val : hm2.entrySet()) {
        System.out.println("Element " + val.getKey() + " "
                + "occurs"
                + ": " + val.getValue() + " times");
        k++;
        if(k==n)
            break;
    }
}
public static void main(String[] args) {
    String nr1;
    ArrayList<String> nr = new ArrayList<String>();
    Scanner obj= new Scanner(System.in);
    System.out.println("Input n");
    int n= obj.nextInt();

    System.out.println("Input k ");
    int k= obj.nextInt();
    System.out.println("Input n elemente: ");
    obj.nextLine(); // Consuming the space
    for(int i=1;i<=n;i++)
    {
        nr1=obj.nextLine();
        System.out.println("Element " + (i));
        nr.add(nr1);
    }
    countFrequencies(nr,k);
}
  1. You are getting last element as null because your code considering the spaces as input.您获得的最后一个元素为 null,因为您的代码将空格视为输入。 Hence I consumed the extra space.因此我消耗了额外的空间。
  2. And to print it in decreasing order you need another LinkedHashMap which actually maintains the insertion order, and I've used stream to sorted the HashMap by values in reverse Order.要按降序打印它,您需要另一个 LinkedHashMap,它实际上维护插入顺序,我使用 stream 按相反顺序按值对 HashMap 进行排序。

The code:编码:

public class main {
public static void countFrequencies(ArrayList<String> list,int n)
{
    int k=0;
    Map<String, Integer> hm = new HashMap<String, Integer>();

    for (String i : list) {
        Integer j = hm.get(i);
        hm.put(i, (j == null) ? 1 : j + 1);
    }

    for (Map.Entry<String, Integer> val : hm.entrySet()) {
        System.out.println("Element " + val.getKey() + " "
                + "occurs"
                + ": " + val.getValue() + " times");
        k++;
        if(k==n)
            break;
    }
}

public static void main(String[] args)  {
    String nr1;
    ArrayList<String> nr = new ArrayList<String>();
    Scanner obj= new Scanner(System.in);
    System.out.println("Input n");
    int n= obj.nextInt();

    System.out.println("Input k ");
    int k= obj.nextInt();
    System.out.println("Input n elemente: ");
    for(int i=1;i<=n;i++)
    {
            nr1=obj.nextLine();
            System.out.println("Element " + (i));
            nr.add(nr1);
    }
    countFrequencies(nr,k);


}

} }

If I run the code everything goes ok until I input the last element in the list, if do It appears as null, it simply doesn't let me input it, also when I print the frequencies list I need it to show the top k elements after the number of frequencies, in the descending order.如果我运行代码一切正常,直到我输入列表中的最后一个元素,如果它显示为 null,它根本不允许我输入它,当我打印频率列表时,我需要它来显示前 k频率数之后的元素,按降序排列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM