简体   繁体   中英

AS3 How to i turn head of the Vector list?

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM