aspose file tools*
The moose likes Beginning Java and the fly likes store result of queue.poll in arraylist Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "store result of queue.poll in arraylist" Watch "store result of queue.poll in arraylist" New topic
Author

store result of queue.poll in arraylist

fioana elegant
Greenhorn

Joined: Oct 06, 2010
Posts: 16
how can i store result of queue.poll in arraylist???

ArrayList<UrlResponseBean> arrurlInfo = null;
public static ConcurrentLinkedQueue<UrlResponseBean> queue = new ConcurrentLinkedQueue<UrlResponseBean>();

public void run() {
arrurlInfo = new ArrayList<UrlResponseBean>();
while(true)
{
if(queue.size()>=5)
{
for(int i=0;i<queue.size();i++)
{
arrurlInfo=queue.poll();
insertIntoDb(arrurlInfo);
}
}

}
}
Wouter Oet
Saloon Keeper

Joined: Oct 25, 2008
Posts: 2700

Please UseCodeTags when posting code. It will highlight your code and make it much easier to read. It probably will also increase the number of people helping you.

what is wrong with:
But if you want to insert all of them you can use addAll()

"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
Greg Charles
Sheriff

Joined: Oct 01, 2001
Posts: 2848
    
  11

Moved to our Beginning Java forum.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19670
    
  18

Wouter Oet wrote:But if you want to insert all of them you can use addAll()

Followed by clear(). After all, pop() removes the element.

On a side note, the for-loop looks dangerous to me, since the body changes the result of queue.size(). Consider an initial queue size of 4:
i == 0, queue.size() == 4 so execute the body. Afterwards, queue.size() == 3.
i == 1, queue.size() == 3 so execute the body. Afterwards, queue.size() == 2.
i == 2, queue.size() == 2 -> quit

If you want to keep the manual loop and poll(), change it to a while loop:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: store result of queue.poll in arraylist