[英]How to print all the elements from a ArrayList?
I'm trying to print all the element I have added to my arraylist, but it only prints the adress and not the string. 我正在尝试打印我添加到我的arraylist中的所有元素,但是它只打印地址而不是字符串。
Can someone help me or give out some tips? 有人可以帮我或给出一些提示吗? I've been searching all afternoon
我整个下午都在搜寻
您需要重写Autuer的toString方法以String格式返回其内容
You can also, use a foreach to do it ;) 您也可以使用foreach来完成它;)
for(Auteur a: auteurs){
System.out.print(a.getName() + " - " + a.getNumber());
}
Every object in Java inherits Java中的每个对象都继承
public String toString();
from java.lang.Object
从
java.lang.Object
in your Auteur
class you need to write some code similar to the following: 在您的
Auteur
类中,您需要编写一些类似于以下内容的代码:
.... .... .... ....
@Override
public String toString() {
return "Name: "+this.name;
}
Try defining the toString()
method in your Auter
class as follows: 尝试如下在
Auter
类中定义toString()
方法:
public String toString() {
return this.getName() + " - " + this.getNumber());
}
and your code will do what you wish. 并且您的代码将按您的意愿执行。
System.out.println
calls the argument's toString()
method and prints that out to the Output console. System.out.println
调用参数的toString()
方法并将其输出到输出控制台。
itr.next()
returns object of Auteur
rather than String
. itr.next()
返回Auteur
对象,而不是String
。 To print the name you need to type cast it with Auteur
and then print it if you have a print method for the class Auteur
. 要打印您需要输入与投它的名字
Auteur
,然后打印出来,如果你有该类的打印方法Auteur
。
Auteur aut = (Auteur) itr.next();
System.out.println(aut.printMethod());
What you see is called the default toString of an object. 您所看到的称为对象的默认toString 。 It is an amalgamation of the FQCN (fully qualified class name) of the class it belongs to and the hashCode of the object.
它是其所属类的FQCN(完全限定的类名)和该对象的hashCode的组合。
Quoting from the JavaDoc of toString: 引用toString的JavaDoc:
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object.
Object类的toString方法返回一个字符串,该字符串包括该对象是其实例的类的名称,符号字符“ @”以及该对象的哈希码的无符号十六进制表示形式。 In other words, this method returns a string equal to the value of:
换句话说,此方法返回的字符串等于:
getClass().getName() + '@' + Integer.toHexString(hashCode())
We can override toString
to give a more human readable output. 我们可以重写
toString
以提供更易读的输出。 Take a look at the below two classes, with and without toString
. 看一下下面的两个类,有和没有
toString
。 Try to execute the main method and compare the output of the two print statements. 尝试执行main方法并比较两个打印语句的输出。
class Person {
private String name;
@Override
public String toString() {
return "Person [name=" + this.name + "]";
}
}
class Address {
private String town;
}
public class Test {
public static void main(String... args) {
Person person = new Person();
Address address = new Address();
System.out.println("Person is : " + person);
System.out.println("Address is : " + address);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.