如何迭代通过嵌套类? [英] How to iterate through nested classes?
问题描述
另一个简单的一个!
我想在Android项目中迭代嵌套类。
I'm trying to iterate through nested classes in an android project.
类结构是:
public class clsMessage {
public String Username = "";
public String Password = "";
public String Request = "";
public class Parameters {
public String Name = "";
public String Value = "";
}
}
外部类实例化为msg。然后我想我会这样询问:
The outer class is instantiated as "msg". Then I thought I'd query it like this:
for (clsMessage.Parameters params : msg) {
// do stuff
}
但这不起作用,我找不到例如在谷歌令人惊讶,除非我应该使用一个迭代器在外部类
But that doesn't work and I can't find an example on Google surprisingly, unless I should be using an iterator in the outer class?
推荐答案
我猜你正在寻找东西如下所示。
I am guessing you are looking for something like the following.
public class ClsMessage
{
public String username = "";
public String password = "";
public String request = "";
public List<Parameters> parameters = new ArrayList<Parameters>();
public static class Parameters {
public String name = "";
public String value = "";
}
}
for(ClsMessage.Parameters param : clsMessage.parameters)
{
....
}
只是声明一个内部类实际上不会有一个inner class你需要为它做一个字段。
考虑到你想用一个for循环迭代它,我假设你想要多于一个 Parameters
在你的消息,所以我添加了一个列表
。
Just declaring an inner class won't actually have an "inner class" as a field. You would need to make a field for it.
Considering you wanted to iterate through it with a for-loop, I assumed you wanted more than one Parameters
in your message, so I added a List
.
这篇关于如何迭代通过嵌套类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!