AS3 How to i turn head of the Vector list?

大城市里の小女人 提交于 2019-12-11 06:58:29

问题


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


回答1:


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.




回答2:


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


来源:https://stackoverflow.com/questions/45750178/as3-how-to-i-turn-head-of-the-vector-list

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!