为什么程序没有终止?

时间:2018-09-09 15:33:03

标签: java arraylist count

我正在制作一个程序,该程序计算数组列表中元素的频率,然后打印出具有最大频率的元素。但是该程序不会打印任何内容,也不会终止。

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.StringTokenizer;

public class TestClass {
        static class FastReader
        {
            BufferedReader br;
            StringTokenizer st;

            public FastReader()
            {
                br = new BufferedReader(new
                         InputStreamReader(System.in));
            }

            String next()
            {
                while (st == null || !st.hasMoreElements())
                {
                    try
                    {
                        st = new StringTokenizer(br.readLine());
                    }
                    catch (IOException  e)
                    {
                        e.printStackTrace();
                    }
                }
                return st.nextToken();
            }

            int nextInt()
            {
                return Integer.parseInt(next());
            }

            long nextLong()
            {
                return Long.parseLong(next());
            }

            double nextDouble()
            {
                return Double.parseDouble(next());
            }

            String nextLine()
            {
                String str = "";
                try
                {
                    str = br.readLine();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                return str;
            }
            void close() {
                try {
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        public static void main(String sfds[]) throws Exception {
            FastReader s= new FastReader();
            int N = s.nextInt();
            ArrayList<String> comp = new ArrayList<>();
            for(int i=0; i<N; i++)
                comp.add(s.next());

            int count = 0;
            String ele="";
            while(comp.size()>0){
                String e = comp.get(0);
                int c = Collections.frequency(comp, e);
                if(c>count){
                    comp.removeAll(Collections.singleton(e));
                    count=c;
                    ele=e;
                }
            }
            System.out.print(ele);
        }


}

我正在删除所有已计数元素的出现。有人可以解释为什么吗?

1 个答案:

答案 0 :(得分:0)

问题与您的代码段:

  • 仅当频率大于当前计数值(导致while循环无限期运行)时才从列表中删除元素。即使频率不超过计数,也要删除这些元素。

        while (comp.size() > 0) {
        String e = comp.get(0);
        int c = Collections.frequency(comp, e);
        if (c > count) {
            count = c;
            ele = e;
        }
        comp.removeAll(Collections.singleton(e));
    }
    System.out.print(ele);
    

进行此微小更改后,您的代码可以正常工作。 输入:8 1 1 1 2 3 1 2 3 输出: 1

相关问题