Results 1 to 2 of 2
- 11-15-2011, 12:08 AM #1
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
Creating Objects within While Loops
I'm trying to get UDP packets and store them in an ArrayList<DatagramPacket> within a while loop. However I feel like I'm adding a reference to a packet so that every object in the array just points to the last packet received. Here's what I'm working with:
DatagramPacket receivedPacket = null;
While (!finished) {
receivedPacket = new DatagramPacket(buf, buf.length);
socket.receive(receivedPacket); //socket is a DatagramSocket and this method changes the buffer within receivedPacket to contain the correct data from the current incoming packet.
packets.add(receivedPacket); //packets is an ArrayList<DatagramPacket>
}
When I then try to use the DatagramPackets that should be in the array, they're all the same. I check using syso's to make sure they're not the same before they're added.
I feel like an idiot that I can't figure this out after years of Java. Please help me.
- 11-15-2011, 12:19 AM #2
Member
- Join Date
- Feb 2011
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
Creating files using loops
By lakshmibvaraprasad in forum New To JavaReplies: 2Last Post: 06-07-2011, 05:09 PM -
HELP - Creating OPTIONS in program and LOOPS
By whateverme in forum New To JavaReplies: 14Last Post: 12-07-2010, 05:55 AM -
creating dynamic objects?
By alacn in forum New To JavaReplies: 4Last Post: 06-04-2010, 03:51 AM -
Creating an array of objects
By geowizard in forum New To JavaReplies: 5Last Post: 11-16-2009, 01:25 AM -
trouble creating program using loops for multiplication table
By cuse17 in forum New To JavaReplies: 2Last Post: 02-23-2009, 02:18 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks