如何在 Java 中打印集合中的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20366363/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 01:25:28  来源:igfitidea点击:

How to print content in Collection in Java

javalistcollectionsstruts

提问by Panadol Chong

Good day All,

大家好,

Normally, I will print all the content in List by look the list.size()and assign it to an object and print the object value. The following is my example code:

通常,我会通过查看列表中的所有内容list.size()并将其分配给一个对象并打印对象值。以下是我的示例代码:

List ccUserList = new ArrayList(); // Here I declare a List
Collection ccGroupCol = new ArrayList(); // Here I declare a collection

CCuserBO bo = null;
ccUserList = getSummaryList();
for(int i = 0, i < ccUserList.size() , i++){
    bo = ( CCUserBO ) ccUserList.get(i);
    System.out.println(bo.userName);
}

I would like to ask about the way to print content in Collection. Since Collection no have .get() function.

我想问一下在Collection中打印内容的方式。由于 Collection 没有 .get() 函数。

The following in the code that I try in Collection:

我在 Collection 中尝试的代码中的以下内容:

    CCuserBO newBo = null;    
    ccGroupCol = getSummaryList();
    Iterator iterator = ccGroupCol.iterator();
                while ( iterator.hasNext()){
                newBo = iterator.next(); //error here, Type mismatch: cannot convert from Object to //Object[]
                    System.out.println("....");
                }

回答by Panadol Chong

I found the solution. Here is the example code:

我找到了解决方案。这是示例代码:

CCGroupBO newBo;
        for(int i = 0 ; i < ccGroupCol.size() ; i++){
            newBo = ( CCGroupBO ) ccGroupCol.toArray()[i];
            System.out.println(newBo.getGroupName());
        }

Thanks for all your help.

感谢你的帮助。

回答by Shailesh Saxena

If you simply want to print all elements of a Collection just sysout Collection directly it will provide you the following form in output: [element1, element2, ....] because toString() method is overrided and implemented to provide such output for all Collection classses.

如果您只是想直接打印集合的所有元素,只需直接 sysout 集合,它将在输出中为您提供以下形式:[element1, element2, ....] 因为 toString() 方法被覆盖并实现为所有提供此类输出收藏类。

By using Iterator you can get the element one by one:

通过使用 Iterator 可以一一获取元素:

    Iterator iterator = ccGroupCol.iterator();
            while ( iterator.hasNext()){
         newBo = (**type cast here to particular newBo object type**)iterator.next(); 
                System.out.println(newBo);//here whatever you implemented in toString() method
    // in newBo type class(if you did so), you will get that type of output, if you do not override
//toString() to provide your implementation,you will get default implementation in
//which it will show <the object class>@<its hash code>
        }

Note: the return type of iterator.next() is Object type, so you must type cast it to avoid incompatible type exception. Or use Generics.

注意:iterator.next() 的返回类型是 Object 类型,所以你必须对它进行类型转换以避免不兼容的类型异常。或者使用泛型。

回答by Pradeep Kr Kaushal

You can use the for loop to iterate the collection.

您可以使用 for 循环来迭代集合。

Collection collection= new ArrayList();
for (Object obj : collection) {
//Here you can type cast the obj then you can print.

}

回答by Jan

As statet in comment, a faster solution for your own answer:

正如评论中的陈述,您自己的答案的更快解决方案:

Collection<CCGroupBO> ccGroupCol = new ArrayList<CCGroupBO>()
…
CCGroupBO[] boArray = ccGroupCol.toArray();
for(CCGroupBO newBo : boArray){
    System.out.println(newBo.getGroupName());
}

or even more direct:

甚至更直接:

Collection<CCGroupBO> ccGroupCol = new ArrayList<CCGroupBO>()
…
for(CCGroupBO newBo : ccGroupCol){
    System.out.println(newBo.getGroupName());
}

depending on other circumstances there is even a nicer method:

根据其他情况,还有更好的方法:

class CCGroupBO {
  …
  public String toString() {
    return getGroupName();
  }
}
…
Collection<CCGroupBO> ccGroupCol = new ArrayList<CCGroupBO>()
…
System.out.println(ccGroupCol);