AS3如何转向矢量列表的头部?

时间:2017-08-18 06:47:39

标签: arrays actionscript-3 flash vector server

我有一个用于ip和端口列表和套接字连接的Vector,当连接丢失时我点击按钮并从向量列表中调用下一个ip和端口。

我的问题是在完成我的清单后如何转为清单的头部?

这是我目前的代码

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

由于

2 个答案:

答案 0 :(得分:1)

在当前实现中,您只能遍历列表一次。您需要更改代码以保持列表不被修改:

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

现在要获取您致电const firstConn:SocketConnection = iterator.first的第一个条目并获取其余条目,请继续致电iterator.next

答案 1 :(得分:1)

需要对您的代码进行一些小调整:

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