Java 如何为对象的 ArrayList 创建 toString 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22158613/
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 do I create a toString method for an ArrayList of objects?
提问by user3241721
I am tasked with creating a toString()
method for each and every object in an ArrayList
. I have no idea how to go about doing this. This is the class with the ArrayList
我的任务是toString()
为ArrayList
. 我不知道该怎么做。这是与ArrayList
public class DogManager {
private ArrayList<Dog> dogList;
public DogManager() {
this.dogList = new ArrayList<Dog>();
}
public void addDog(String nameOfDog) {
this.dogList.add(new Dog(nameOfDog));
}
public String toString() {
String results = "+";
for (int i = 0; i < this.dogList.size(); i++) {
results += " " + this.dogList.get(i);
}
return results;
}
}
I know the toString()
is wrong, but I can't figure out how to make it return a description for each of the objects in that list.
我知道这toString()
是错误的,但我不知道如何让它返回该列表中每个对象的描述。
采纳答案by trevor-e
You are close. The easiest way I can think of is to also implement toString()
for Dog
. Then in your DogManager
class you can loop through each Dog
and call its toString()
.
你很近。我能想到的最简单的方法是同时实现toString()
for Dog
. 然后在您的DogManager
班级中,您可以遍历每个Dog
并调用其toString()
.
ie:
IE:
public String toString() {
String results = "+";
for(Dog d : dogList) {
results += d.toString(); //if you implement toString() for Dog then it will be added here
}
return results;
}
}
edit: You can also format it however you like. I notice some answers separate each Dog by ","
编辑:您也可以随意格式化。我注意到一些答案将每只狗用“,”分开
回答by Solace
public String toString(){
String toReturn = "[";
for(Dog currentDog: dogList){
toReturn+=currentDog.toString()+",";
}
toReturn = toReturn.substring(0,toReturn.length()-1);
return toReturn+"]";
}
回答by RMachnik
public String toString() {
String results = "";
for(Dog d : dogList) {
results += "," + d.toString();
}
return results;
}
}
回答by Jurko Guba
An arraylist is a dynamic array filled with Objects. Why don't you overwrite the toString method in the Dog class.
arraylist 是一个充满对象的动态数组。为什么不覆盖 Dog 类中的 toString 方法。
回答by Warlord
You need to define .toString()
method in your Dog
class and make it override the default .toString()
method of Object
class.
您需要.toString()
在Dog
类中定义方法并使其覆盖类的默认.toString()
方法Object
。
public class Dog {
String name;
String age;
String race;
@Override
public String toString() {
return String.format("Name: %s, Age: %s, Race: %s", name, age, race);
}
}
Then you can simply call System.out.println(dog)
for each element of the array and your custom text defined in dog's .toString()
method will be displayed.
然后你可以简单地调用System.out.println(dog)
数组的每个元素,你在 dog.toString()
方法中定义的自定义文本就会显示出来。
回答by Warlord
I would do something like this, no need to loop through the list of dogs (a List.toString
already does that for you).
我会做这样的事情,不需要遍历狗的列表(aList.toString
已经为你做了)。
public class Dog {
private String name;
@Override
public String toString() {
return "Dog{" + name + "}";
}
}
public class DogManager {
private List<Dog> dogs;
@Override
public String toString() {
return "DogManager{dogs=" + dogs + "}";
}
}