我试图打印出一个列表的所有元素,但它是打印对象的指针,而不是值。

这是我的打印代码…

for(int i=0;i<list.size();i++){
    System.out.println(list.get(i));
} 

有人能告诉我为什么它不打印元素的值吗?


当前回答

System.out.println(list);//toString() is easy and good enough for debugging.

AbstractCollection的toString()将非常干净和容易做到这一点。AbstractList是AbstractCollection的子类,因此不需要for循环,也不需要toArray()。

返回此集合的字符串表示形式。字符串表示形式由集合元素的列表组成 它们由迭代器返回的顺序,用方括号括起来 (“[]”)。相邻元素用字符“,”(逗号)分隔 和空间)。通过将元素转换为字符串 String.valueOf(对象)。

如果您正在使用列表中的任何自定义对象,例如Student,则需要重写它的toString()方法(重写这个方法总是好的)以获得有意义的输出

请看下面的例子:

public class TestPrintElements {

    public static void main(String[] args) {

        //Element is String, Integer,or other primitive type
        List<String> sList = new ArrayList<String>();
        sList.add("string1");
        sList.add("string2");
        System.out.println(sList);

        //Element is custom type
        Student st1=new Student(15,"Tom");
        Student st2=new Student(16,"Kate");
        List<Student> stList=new ArrayList<Student>();
        stList.add(st1);
        stList.add(st2);
        System.out.println(stList);
   }
}


public  class Student{
    private int age;
    private String name;

    public Student(int age, String name){
        this.age=age;
        this.name=name;
    }

    @Override
    public String toString(){
        return "student "+name+", age:" +age;
    }
}

输出:

[string1, string2]
[student Tom age:15, student Kate age:16]

其他回答

public static void main(String[] args) {
        answer(10,60);

    }
    public static void answer(int m,int k){
        AtomicInteger n = new AtomicInteger(m);
        Stream<Integer> stream = Stream.generate(() -> n.incrementAndGet()).limit(k);
        System.out.println(Arrays.toString(stream.toArray()));
    }

使用String.join () 例如:

System.out.print(String.join("\n", list));

考虑一个List<String> stringList,它可以使用Java 8结构以多种方式打印:

stringList.forEach(System.out::println);                            // 1) Iterable.forEach
stringList.stream().forEach(System.out::println);                   // 2) Stream.forEach (order maintained generally but doc does not guarantee)
stringList.stream().forEachOrdered(System.out::println);            // 3) Stream.forEachOrdered (order maintained always)
stringList.parallelStream().forEach(System.out::println);           // 4) Parallel version of Stream.forEach (order not maintained)
stringList.parallelStream().forEachOrdered(System.out::println);    // 5) Parallel version ofStream.forEachOrdered (order maintained always)

这些方法之间有什么不同?

第一种方法(Iterable.forEach)- 通常使用集合的迭代器,它被设计为快速失败,这意味着如果底层集合在迭代期间被结构修改,它将抛出ConcurrentModificationException。正如在ArrayList的文档中提到的:

结构修改是添加或删除一个或 更多的元素,或者显式地调整支持数组的大小;只是设置 元素的值不是结构修改。

它的意思是数组列表。对于每个设置值都是允许的,没有任何问题。在并发收集的情况下,例如ConcurrentLinkedQueue,迭代器将是弱一致的,这意味着在forEach中传递的操作甚至可以在不抛出ConcurrentModificationExceptionexception的情况下进行结构更改。但是这里的修改可能在迭代中可见,也可能不可见。

第二种方法(Stream.forEach)- 顺序没有定义。虽然它可能不会出现在连续流中,但规范并不能保证它。此外,该行为还必须是不干涉性质的。如doc中所述:

此操作的行为明显是非确定性的。为 平行流管道,此操作不保证 尊重溪流的遭遇顺序,因为这样做会牺牲 并行的好处。

第三种方法(Stream.forEachOrdered)- 该操作将按照流的遇到顺序执行。因此,每当顺序问题时,请使用forEachOrdered。正如文件中提到的:

在相遇中为此流的每个元素执行一个操作 如果流具有定义的遇到顺序,则为流的顺序。

当迭代一个同步集合时,第一种方法将获得集合的锁一次,并在所有对action方法的调用中保持它,但在流的情况下,他们使用集合的spliterator,它不锁,依赖于已经建立的不干扰规则。如果在迭代过程中修改了支持流的集合,则会抛出ConcurrentModificationException异常或可能出现不一致的结果。

第四种方法(Parallel Stream.forEach)- 正如已经提到的,在并行流的情况下,不能保证尊重遇到的顺序。有可能在不同的线程中对不同的元素执行不同的操作,而forEachOrdered则永远不会出现这种情况。

第五种方法(Parallel Stream.forEachOrdered)- forEachOrdered将按照源指定的顺序处理元素,而不管流是顺序的还是并行的。所以在并行流中使用这个是没有意义的。

或者你可以简单地使用Apache Commons实用程序:

https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/ArrayUtils.html#toString-java.lang.Object-

List<MyObject> myObjects = ...
System.out.println(ArrayUtils.toString(myObjects));

从Java 8开始,List继承了一个默认的“forEach”方法,你可以将它与方法引用“System”结合起来使用。输出::println”像这样:

list.forEach(System.out::println);