![](/img/trans.png)
[英]Why does System.out.println print a new line while System.out.print prints nothing?
[英]Why System.out.println prints the elements of an arraylist and not the hashcode of the object?
我沒有重寫toString()
所以很困惑。 ArrayList
不是像數組這樣的對象,因為它們是使用new
創建的?
例:
ArrayList <String> arri= new ArrayList();
String one="one";
String two=new String ("two");
arri.add(one);
arri.add(two);
System.out.println(arri);
//output:
//[one, two]
謝謝
您不必重寫toString()
...調用它的對象確實可以。 您在ArrayList
上調用toString()
,並且ArrayList
覆蓋toString
...或更確切地說, AbstractCollection
操作 ,而ArrayList
繼承了實現:
返回此集合的字符串表示形式。 字符串表示形式包括一個集合元素的列表,這些元素按其迭代器返回的順序排列,並括在方括號(“ []”)中。 相鄰元素由字符“,”(逗號和空格)分隔。 元素通過
String.valueOf(Object)
轉換為字符串。
ArrayList
(與數組不同)覆蓋toString()
而String
覆蓋toString()
,因此打印ArrayList<String>
會顯示該List
所有String
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.