java 这个程序中的 ArrayList 如何在没有任何循环的情况下打印元素?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28740821/
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
How ArrayList in this program printing the elements without any loop?
提问by Sachin Rana
How does the following program prints the element contained in ArrayList without using any loop
以下程序如何在不使用任何循环的情况下打印 ArrayList 中包含的元素
import java.util.*;
class Abc{
public static void main(String args[]){
ArrayList<String> al=new ArrayList<String>();
al.add("Ravi");
al.add("Vijay");
al.add("Ravi");
al.add("Ajay");
System.out.println(al);
}
}
Output : [Ravi, Vijay, Ravi, Ajay]
输出:[拉维,维杰,拉维,阿杰]
回答by TheLostMind
ArrayList
by itself doesn't have a toString()
method. It extends AbstractList
which inturn extends AbstractCollection
which defines the actual 'toString'method.
ArrayList
本身没有toString()
方法。它扩展了它又扩展了AbstractList
它AbstractCollection
定义了实际的“toString”方法。
From java docs :
来自 java 文档:
toString
public String toString()
Returns a string representation of this collection. The string representation consists of a list of the collection's elements in the order they are returned by its iterator, enclosed in square brackets ("[]"). Adjacent elements are separated by the characters ", " (comma and space). Elements are converted to strings as by String.valueOf(Object).
返回此集合的字符串表示形式。字符串表示由集合元素的列表组成,按其迭代器返回的顺序排列,括在方括号(“[]”)中。相邻元素由字符“,”(逗号和空格)分隔。元素通过 String.valueOf(Object) 转换为字符串。
Overrides:
toString in class Object
Returns:
a string representation of this collection
回答by Kevvvvyp
Think of the line:
想想这条线:
System.out.println(al);
As really being
作为真正的
System.out.println(al.toString());
The toString method is inherited from AbstractCollection.
toString 方法继承自 AbstractCollection。
If your interested in what is going on behind the AbstractCollection's toString method ...
如果您对 AbstractCollection 的 toString 方法背后发生的事情感兴趣......
链接:http: //grepcode.com/file/repository.grepcode.com/java/root/jdk/openjdk/6-b14/java/util/AbstractCollection.java#AbstractCollection.toString%28%29
回答by RealSkeptic
It is using a loop, but not in the code that you are showing, but in the method toString()
that ArrayList
inherits from AbstractCollection
which is one of its ancestor classes.
它正在使用循环,但不是在您显示的代码中,而是在继承自其祖先类之一的方法toString()
中。ArrayList
AbstractCollection
Whenever you pass an object to print()
or println()
, what actually happens is that print()
calls String.valueOf()
passing it that object.
每当您将对象传递给print()
or 时println()
,实际发生的是print()
调用String.valueOf()
将对象传递给它。
This, in turn, if the object passed is not null, calls the objects
toString` method.
这反过来,如果传递的对象不为空,则调用对象的s
toString` 方法。
AbstractCollection
has a toString
method that relies on iterating and calling the toString
methods of each item inside the collection it represent.
AbstractCollection
有一个toString
方法依赖于迭代和调用toString
它所代表的集合中每个项目的方法。
So there is a loop there, but it`s buried inside a method that calls a method that calls a method.
所以那里有一个循环,但它被埋在一个调用一个方法的方法中。
回答by Android Team
Because ArrayList is written in Java which has it's own implementation. So that's why when you user toString() then it will print your elements.
因为 ArrayList 是用 Java 编写的,它有自己的实现。所以这就是为什么当您使用 toString() 时它会打印您的元素。
But if you try to use toString() with Array then it will not work. Because array is part of languate specification.
但是,如果您尝试将 toString() 与 Array 一起使用,则它将不起作用。因为数组是语言规范的一部分。
Hope this will helps you :)
希望这会帮助你:)