AS3 如何转为矢量列表的头部?
AS3 How to i turn head of the Vector list?
我有一个用于 ip 和端口列表以及套接字连接的向量,当连接丢失时,我单击按钮并从向量列表中调用下一个 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;
}
}
谢谢
在当前的实现中,您只能遍历列表一次。您需要更改代码以保持列表不变:
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
。
需要对您的代码进行小的调整:
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;
}
我有一个用于 ip 和端口列表以及套接字连接的向量,当连接丢失时,我单击按钮并从向量列表中调用下一个 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;
}
}
谢谢
在当前的实现中,您只能遍历列表一次。您需要更改代码以保持列表不变:
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
。
需要对您的代码进行小的调整:
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;
}