gpt4 book ai didi

java - 更新存储在阻塞优先级队列中的自定义对象

转载 作者:太空宇宙 更新时间:2023-11-04 08:40:09 25 4
gpt4 key购买 nike

我有一个阻塞优先级队列,它存储消息类型的对象,消息有 String[] data = new String[10]。现在我必须迭代整个阻塞队列,检查其对象消息的第二个元素是否等于传入消息的第六个元素。

消息的比较器不是基于需要更新的第6个元素。问题是,如果我取出一个对象,那么如何将它放在相同的位置,如果我使用下面的代码来更新它,那么每当运行 iter.next() 时,它可能会开始指向下一个对象。

这就是我正在尝试的。

public synchronized void updateAck(Message ackMessage)
{
Iterator iter = localQ.iterator(); // localQ is the blocking priority queue here
while(iter.hasNext())
{
if(((Message)iter.next()).data[2].equalsIgnoreCase(ackMessage.data[6]))
{
(Integer.parseInt((Message)iter.next()).data[6])+1);

}
}
}

最佳答案

不要在 if 条件中直接使用 (Message)iter.next(),而是尝试这样做。

Message queMessage = (Message)iter.next();

完整代码

 while(iter.hasNext())
{

Message queMessage = (Message)iter.next(); //you will be invoking .next() only once

if(queMessage.data[2].equalsIgnoreCase(ackMessage.data[6]))
{
(Integer.parseInt(queMessage.data[6])+1);

}
}

关于java - 更新存储在阻塞优先级队列中的自定义对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5508983/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com