I have a Vector for ip and port list and socket connection, When connection lose i click button and call the next ip and port from vector list.
My question is when finish my list how to i turn head of the list ?
This my current code
public class UriIterator
{
private var _availableAddresses: Vector.<SocketConnection> = new Vector.<SocketConnection>();
public function UriIterator()
{
}
public function withAddress(host: String, port: int): UriIterator {
const a: SocketConnection = new SocketConnection(host, port);
_availableAddresses.push(a);
return this;
}
public function get next(): SocketConnection{
return _availableAddresses.length ? _availableAddresses.pop() : null;
}
}
Thanks
In current implementation you can traverse the list only once. You need to change the code to keep the list unmodified:
public class UriIterator
{
private var _currentIndex:int = 0;
private var _availableAddresses: Vector.<SocketConnection> = new Vector.<SocketConnection>();
public function withAddress(host: String, port: int): UriIterator {
const a: SocketConnection = new SocketConnection(host, port);
_availableAddresses.push(a);
return this;
}
public function get first():SocketConnection {
_currentIndex = 0;
if (_currentIndex >= _availableAddresses.length)
return null;
return _availableAddresses[_currentIndex++];
}
public function get next(): SocketConnection {
if (_currentIndex >= _availableAddresses.length)
return null;
return _availableAddresses[_currentIndex++];
}
}
Now to get the first entry you call const firstConn:SocketConnection = iterator.first
and to get the rest of them, just keep calling iterator.next
.
A small tweak to your code needed:
public function get next():SocketConnection
{
// Get the next one.
var result:SocketConnection = _availableAddresses.pop();
// Put it back at the other end of the list.
_availableAddresses.unshift(result);
return result;
}