Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

While browsing the vector, can you delete elements?

Status
Not open for further replies.

ankursaxena

Programmer
Sep 7, 2001
133
US
Hi, I have this piece of code which i dont know will work fine. I am still writting code, and dont have a way of testing, may be someone can tell me if I can remove an element which is in the iteration and then cont, if I cont, what will the next element be?

Thanks

Code:
		    	ListIterator iter = report_Thread.outStreams.listIterator();
		    	ListIterator iter_sock = report_Thread.clientSockets.listIterator();
		    	int o_count = 0;
		    	while (iter.hasNext())
		    	{
		    		try
		    		{
		    			Socket sock = (Socket)iter_sock.next();
		    			ObjectOutputstream oos = (ObjectOutputStream)iter.next();
		    			if(sock.isConnected())
		    			{
		    				oos.writeChars(ids);
		    				oos.writeChars(vals);
		    				oos.flush();	
		    			}
		    			else
		    			{
		    				//remove sock and steam
		    				report_Thread.outStreams.remove(oos);
		    				report_Thread.clientSockets.remove(sock);
		    				AppConstants.traceln("Removed socket/stream from vectors because conn was closed.");
		    			}
		    		}
		    		catch(Exception exc)
		    		{
		    			AppConstants.traceln("Exception occured while sending to socket " + o_count);
		    			exc.printStackTrace(AppConstants.log_file);
		    		}
 
Well you will just remove the element from the vector. It will have no effect on the iterator because you created the iterator from the vector before you started looping the iterator object.

PS, in the future, this forum is for J2EE issues, the standard Java forum is here : forum269

--------------------------------------------------
Free Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top