[英]How does (this) overriding tostring method for LinkedList class work?
I have created a class Test
storing class Pair
objects in linkedlist data structure defined in the library . 我创建了一个
class Test
将class Pair
对象存储在库中定义的链表数据结构中。 To print the Pair
class objects I have overridden method in pair class and it works as shown below - 为了打印
Pair
类对象,我在Pair
类中重写了方法,它的工作原理如下所示-
import java.util.LinkedList;
class Test{
static LinkedList<Pair> list=new LinkedList<Pair>();
public static void main(String[] args){
list.add(new Pair(31,78));
list.add(new Pair(89,67));
list.add(new Pair(90,43));
System.out.println(list);
}
}
class Pair{
int x;
int y;
Pair(int m, int n){
x=m;
y=n;
}
@Override
public String toString(){
return "{ "+ this.x + ", " +this.y+" }";
}
}
The output - 输出 -
[{ 2, 4 }, { 6, 3 }, { 12, 6 }]
My doubt is regarding the toString()
method- 我的疑问是关于
toString()
方法的-
The implementation of LinkedList<E>.toString()
is inherited from AbstractCollection<E>
; LinkedList<E>.toString()
是从AbstractCollection<E>
继承的; it's documented like this: 它的记录如下:
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 byString.valueOf(Object)
.元素通过
String.valueOf(Object)
转换为字符串。
So String.valueOf
is called on each element - which in turn will call Object.toString()
for any non-null element. 因此,在每个元素上调用
String.valueOf
反过来,它将为任何非null元素调用Object.toString()
。 In your case, you've overridden that in Pair
, so that's what's called. 在您的情况下,您已经在
Pair
覆盖了它,所以这就是所谓的。
If you add some logging in Pair.toString
, like this: 如果在
Pair.toString
添加一些日志记录,如下所示:
public String toString(){
String ret = "{ "+ this.x + ", " +this.y+" }";
System.out.println("toString called: returning " + ret;
return ret;
}
... you can see it being called (or you could use a debugger). ...您可以看到它被调用了(或者您可以使用调试器)。 And yes, if the list contained integers, the list's string representation would be "[1, 2, 3, 4, 5]" or whatever.
是的,如果列表包含整数,则列表的字符串表示形式将为“ [1、2、3、4、5]”或其他任何形式。 (It's possible that
String.valueOf
is optimized to handle numeric types directly, but logically it would call the toString()
override in Integer
.) (可能将
String.valueOf
优化为直接处理数字类型,但是从逻辑上讲 ,它将调用Integer
的toString()
覆盖。)
Here is the source code for LinkedList: 这是LinkedList的源代码:
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/LinkedList.java http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/LinkedList.java
Which (after a few steps) ends up implementing AbstractCollection 哪一步(经过几个步骤)最终实现了AbstractCollection
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/AbstractCollection.java http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/util/AbstractCollection.java
And as you can see the source code for toString
is here: 如您所见,
toString
的源代码在这里:
public String toString() {
Iterator<E> it = iterator();
if (! it.hasNext())
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = it.next();
sb.append(e == this ? "(this Collection)" : e);
if (! it.hasNext())
return sb.append(']').toString();
sb.append(',').append(' ');
}
}
Now we need to find out what StringBuilder
does when adding e
using .append()
现在我们需要找出使用
.append()
添加e
时StringBuilder
功能。
Which is found in the StringBuilder source 可在StringBuilder源代码中找到
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/StringBuilder.java http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/StringBuilder.java
@Override
public StringBuilder append(Object obj) {
return append(String.valueOf(obj));
}
And now the question is, what does String.valueOf
do? 现在的问题是,
String.valueOf
做什么?
Here is the source: 来源如下:
http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/String.java http://hg.openjdk.java.net/jdk8/jdk8/jdk/file/687fd7c7986d/src/share/classes/java/lang/String.java
public static String valueOf(Object obj) {
return (obj == null) ? "null" : obj.toString();
}
So the answer to your question is: Yes, it would have called Integer
's toString
method. 因此,您的问题的答案是:是的,它将调用
Integer
的toString
方法。
Does object class uses the end class's toString() method to print
对象类是否使用结束类的toString()方法进行打印
No, the println
method calls the list
objects toString()
method. 不,
println
方法将list
对象调用为toString()
方法。
The LinkedList
has a toString()
implementation that calls the toString()
method of all the objects in the list to build the [item1, item2, ...]
string. LinkedList
具有toString()
实现,该实现调用列表中所有对象的toString()
方法以构建[item1, item2, ...]
字符串。
Which of course means that your toString()
method is called by LinkedList
, not by Object
class. 当然,这意味着您的
toString()
方法是由LinkedList
而不是由Object
类调用的。
If I would have stored some integers in a linked list, would
Integer
classtoString()
method would have been called?如果我将一些整数存储在链表中,是否会调用
Integer
类toString()
方法?
Yes. 是。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.