簡體   English   中英

為什么列表中的最后一個元素顯示為 null

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

代碼:

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);


}

}

如果我運行代碼,一切正常,直到我輸入列表中的最后一個元素,如果它顯示為 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. 您獲得的最后一個元素為 null,因為您的代碼將空格視為輸入。 因此我消耗了額外的空間。
  2. 要按降序打印它,您需要另一個 LinkedHashMap,它實際上維護插入順序,我使用 stream 按相反順序按值對 HashMap 進行排序。

編碼:

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);


}

}

如果我運行代碼一切正常,直到我輸入列表中的最后一個元素,如果它顯示為 null,它根本不允許我輸入它,當我打印頻率列表時,我需要它來顯示前 k頻率數之后的元素,按降序排列。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM