迭代 AttributeSet 枚举
我有以下代码:
private static boolean hasTargetStyle(AttributeSet attributes) {
final Enumeration<?> attributeNames = attributes.getAttributeNames();
while (attributeNames.hasMoreElements()) {
final Object attributeName = attributeNames.nextElement();
if (attributeName.equals(MY_STYLE_NAME)) {
return true;
}
}
return false;
}
现在我认为这段代码将逐步遍历每个属性名称。但它只给了我所有其他属性名称(具有偶数索引的属性名称)。
这里出了什么问题?
I have the following code:
private static boolean hasTargetStyle(AttributeSet attributes) {
final Enumeration<?> attributeNames = attributes.getAttributeNames();
while (attributeNames.hasMoreElements()) {
final Object attributeName = attributeNames.nextElement();
if (attributeName.equals(MY_STYLE_NAME)) {
return true;
}
}
return false;
}
Now I would think this code would step through each of the attribute names. But it's only giving me every other attribute name (the ones with even-numbered indices).
What's going wrong here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不认为它有索引 -
Set
没有索引。而且代码看起来不错。除非 getAttributeNames() 返回错误实现的枚举,否则它应该可以工作。I don't think it has an index - a
Set
doesn't have index. And the code looks fine. Unless thegetAttributeNames()
returns a wrongly-implementation enumeration, it should work.目前我没有发现您的代码有任何问题,但请尝试使用 Collections.list
I don't see anything wrong with your code at the moment, but try using Collections.list
我怀疑这是
java.util.Enumeration
的问题(尽管这只是一个接口,实际的实现可能有bug)。你的实施对我来说看起来不错。其他想法:初始的 AttributeSet 可能只包含所有其他属性。尝试设置断点并查看实际属性集的内部结构。
I doubt that this is a problem with
java.util.Enumeration
(although this is just an interface and the actual implementation may have a bug). Your implementation looks good to me.Other idea: The initial
AttributeSet
may only contain every other attribute. Try to set a breakpoint and have a look at the internals of an actual attribute set.我在调试器中查看的内部列表的名称和值都是交替的。所以,我的代码在某种意义上是正确的,但我的意图是错误的。
The internal list I was looking at in the debugger had both names and values alternating. So, my code is correct in a certain sense, but my intent was wrong.