简体   繁体   English

在java中,如何迭代对象列表

[英]in java,how to iterate list of objects

i am having the list myEmpls 我有列表myEmpls

List myEmpls = new ArrayList();

In this list i have added used defined objects. 在此列表中,我添加了已使用的已定义对象。

LogConf e = getLogs(el);
    //add it to list
 myEmpls.add(e);

Now how to iterate the list of objects and get the values from this objects. 现在如何迭代对象列表并从这些对象中获取值。 How to do this? 这个怎么做?

You could just google this and you would find tons of solutions.. But here is the code: 你可以谷歌这个,你会发现大量的解决方案..但这里是代码:

for(LogConf element : myEmpls) {
  System.out.println(element.getValue());
}

You should also get used to define the type of the elements in the list: 您还应该习惯于定义列表中元素的类型:

List<LogConf> myEmpls = new ArrayList<LogConf>();

I know its been some time since this post was made. 我知道自从这篇文章发表以来已经有一段时间了。 You can also use the Iterator. 您也可以使用Iterator。

              List myEmpls = new ArrayList();
              Iterator itr = myEmpls.iterator();

              while(itr.hasNext()) {   
                   LogConf Logobj = (LogConf) itr.next();
                   System.out.println(Logobj.getterName());
              }

Hope this helps someone. 希望这有助于某人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM