AS3 如何成为矢量列表的头部?
Posted
技术标签:
【中文标题】AS3 如何成为矢量列表的头部?【英文标题】:AS3 How to i turn head of the Vector list? 【发布时间】:2018-01-26 18:11:56 【问题描述】:我有一个用于 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;
谢谢
【问题讨论】:
【参考方案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;
【讨论】:
感谢我这样解决/private var currentIndex:int = 0; public function get next():SocketConnection var address = _availableAddresses[currentIndex]; currentIndex++; if (currentIndex > _availableAddresses.length - 1) currentIndex = 0; return address;
我的其他问题请帮忙...***.com/questions/45755573/…谢谢
@KaraEski 这不是一个问题,它是对工作代码的请求,但没有显示您迄今为止尝试过的内容。【参考方案2】:
在当前实现中,您只能遍历列表一次。您需要更改代码以保持列表不变:
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
。
【讨论】:
感谢您的回答! @Nbooo 当我单击重新连接按钮时,如何在列表中添加自动下一步?以上是关于AS3 如何成为矢量列表的头部?的主要内容,如果未能解决你的问题,请参考以下文章
Flutter 之列表和头部 (ListView + Header)