I have a question about copying a LinkedList
. I'm working on a project that is taking in data and storing it in a LinkedList
. This is done through开发者_C百科 a live feed via a network port and I want to be able to copy the data from this list to another one so as to process the data collected. Since there is always data being collected is there a way like in C to change the "pointers" so that the head of the first list is now associated with the second LinkedList
were as the first would then be empty for new data?
It sounds to me like what you need is 2 Queues. There's 1 queue for collecting and 1 for processing. At some point, you need to swap them back and forth. As long as you lock briefly for the switch, everything should be ok. There's no need to mess around with the pointers on the linked list, AND you can avoid copying contents around.
That being said, maybe you should consider looking at the concurrency packages for Executors that work off of Queues. Without knowing much about your specific task, it seems like those might be a better match for what you are doing and allow you to avoid having to code the details by hand.
EDIT: This is the package I'm talking about. http://download.oracle.com/javase/1,5.0/docs/api/java/util/concurrent/package-summary.html
second.addAll(first);
first.clear();
What about one of:
linkedlist.push(newHeadValue);
or
linkedlist.addFirst(newHeadValue);
or
linkedlist.add(0,newHeadValue);
or do you explicitly want a step where the head has no data?
精彩评论