【问题标题】:Values in priority queue are deleted as soon as I am outside the function, why?一旦我在函数之外,优先队列中的值就会被删除,为什么?
【发布时间】:2020-05-26 03:04:07
【问题描述】:

我正在调用一个函数来从另一个函数创建优先级队列。

void readparameters() throws NumberFormatException, IOException
{

        try (BufferedReader br = new BufferedReader(new FileReader("FB2010-1Hr-150-0.txt"))) {
            while (br.ready()) {
                Coflow_All.add(br.readLine());
            }
        }
        Framework.size = Coflow_All.size();
        System.out.println("\nThere are "+Framework.size+" coflows entries\n");


        for(queue_entry=1;queue_entry<=15;queue_entry++)
        {
            //Extract a Coflow and satisfy conditions

            ArrayList<Integer> Coflow_each = new ArrayList<Integer>();


            String input = Coflow_All.get(coflow_pick);
            int find=input.indexOf(":");
            if (find != -1) 
            {   
                //this will give substring
                input= input.substring(0 , find); 
            }

            String[] numbers = input.split(" "); 
            for (String s : numbers) 
            {
               Coflow_each.add(Integer.parseInt(s));           
            }   
            //System.out.println(Coflow_each);

            createqueue(Coflow_each.get(0),Coflow_each.get(1),Coflow_each.get(2),Coflow_each.get(4));
            coflow_pick++;

        }

 void createqueue(int id,int Arrivaltime,int mappers_req,int reducers_req) throws NumberFormatException
    {
         Framework.list.clear();


        Collections.addAll(Framework.list, Arrivaltime,mappers_req,reducers_req);
        //System.out.println("The list is "+Framework.list);

        //Analyze the parameters in Framework.list and then do the entry in queue   

        for(int i=1;i<=mappers_req;i++)
        {
        Queue1.add(new Create_queue(id,Framework.list));
        System.out.println(Queue1.size());
        queue_entry++;

        }       
        java.util.Iterator<Create_queue> it = Queue1.iterator();
        while(it.hasNext()) {
            System.out.println(Queue1.poll().toString());
        }
        System.out.println(Queue1.size());
        }
}

但是,一旦我在循环之外,显示的队列大小就为 0。我希望队列的大小只增加而不是 0。请帮忙!!

所有变量都在类中定义,所以不用担心那部分。

【问题讨论】:

  • 是的,但是如果不显示队列是如何生成和处理的,您将不会得到答案。
  • minimal reproducible example 上发布您的问题将是您快速获得严肃和体面答案的最佳选择。请完整阅读链接。
  • 当前代码的格式很差,也无法编译。是的,minimal reproducible example 可以简化问题,代码将再次成为您的最佳选择。
  • 顺便说一句,我强烈建议您开始遵循正常的 Java 命名约定。目前,您的方法和变量的命名方式不一致并且不遵循 Java 约定。
  • 您应该添加更多详细信息,您使用的所有类。并且请使用类和对象的驼峰命名约定! Create_queue 类应该是CreateQueueCoflow_each 变量应该是coflowEach...

标签: java


【解决方案1】:

在我看来,他下面的代码块正在遍历您的集合并删除所有元素。

    java.util.Iterator<Create_queue> it = Queue1.iterator();
    while(it.hasNext()) {
        System.out.println(Queue1.poll().toString());
    }
    System.out.println(Queue1.size());

我认为上面代码的目的是将队列的内容打印到屏幕上,然后显示它包含的元素总数。

但是,由于您正在使用 poll(),它检索并删除项目 (https://docs.oracle.com/javase/7/docs/api/java/util/Queue.html),因此您正在删除所有元素。

要实际打印元素,您需要执行类似于此 SO 答案 Print out all elements of a stack/queue 中所写内容的操作

【讨论】:

  • 非常感谢,这是真正的问题
【解决方案2】:

您正在使用poll(),它通过合同改变队列:返回并从中删除元素。

使用迭代器中的next() 方法来保留元素:

Iterator<Create_queue> it = Queue1.iterator();
while(it.hasNext()) {
    System.out.println(it.next().toString());  // <-- get element and do not remove it
}
System.out.println(Queue1.size()); // will not be 0.

【讨论】:

  • 非常感谢。是的,这就是问题所在,现在我的程序没有返回大小 0。
猜你喜欢
  • 1970-01-01
  • 2012-01-16
  • 2014-01-12
  • 1970-01-01
  • 1970-01-01
  • 2017-11-21
  • 2020-03-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多