Java 打印对象数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29642108/
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
Java printing an array of objects
提问by
I know there are a lot of pages about this question, but I cannot understand it in my case.
我知道有很多关于这个问题的页面,但在我的情况下我无法理解。
I need to print the array of objects. For example, I have an array of objects that hold objects from the "shape" class. Do I call the toString method for each object in the array, or do I code the toString method in ObjectList to print out the instance variables? If so, how do I do that?
我需要打印对象数组。例如,我有一个对象数组,其中包含来自“形状”类的对象。我是为数组中的每个对象调用 toString 方法,还是在 ObjectList 中编写 toString 方法以打印出实例变量?如果是这样,我该怎么做?
public class Shape{
private String shapeName;
private int numSides;
public String toString(){
return shapeName + " has " + numSides + " sides.";
}
}
public class ObjectList{
private Object[] list = new Object[10];
private int numElement = 0;
public void add(Object next){
list[numElement] = next;
}
public String toString(){
// prints out the array of objects
// do I call the toString() method from the object?
// or do I print the instance variables? What am I printing?
// I'm guessing I do a for loop here
}
}
public class Driver{
public static void main(String[] args){
ObjectList list = new ObjectList();
Shape square = new Shape("square", 4);
Shape hex = new Shape("hexagon", 6);
list.add(square);
list.toString(); // prints out array of objects
}
I am aiming for it to print this:
我的目标是打印这个:
square has 4 sides
hexagon has 6 sides
采纳答案by Abubakkar
The simplest way to do this is use Arrays.toString:
最简单的方法是使用 Arrays.toString:
Arrays.toString(myArray);
This will internally call the toString
method of every element of your array.
这将在内部调用toString
数组中每个元素的方法。
So just override toString
method in your Shape
class and it should work fine.
所以只需覆盖toString
你的Shape
类中的方法,它应该可以正常工作。
To add further, override toString
method in your class where you call Arrays.toString
on your variable list
:
要进一步添加,toString
在您调用Arrays.toString
变量的类中覆盖方法list
:
public class ObjectList{
private Object[] list = new Object[10];
.............
public String toString(){
return Arrays.toString(list);
}
}
回答by LuvElDuderino
Write a for-each statement in toString() of Object List and create a large String with '\n' characters and return it as a String . Or may be name displayListElement() will be semantically more correct in which you can simple print all the Objects in the list .
在 Object List 的 toString() 中编写 for-each 语句并创建一个带有 '\n' 字符的大 String 并将其作为 String 返回。或者名称 displayListElement() 在语义上更正确,您可以简单地打印列表中的所有对象。
回答by niks
You can do this with bellowed code, make for loop in toString method to print each shape object.
您可以使用以下代码执行此操作,在 toString 方法中创建 for 循环以打印每个形状对象。
class Shape{
private String shapeName;
private int numSides;
Shape(String shapeName, int numSides){
this.shapeName = shapeName;
this.numSides = numSides;
}
public String toString(){
return shapeName + " has " + numSides + " sides.";
}
}
class ObjectList{
private Object[] list = new Object[10];
private int numElement = 0;
public void add(Object next){
list[numElement] = next;
numElement++;
}
@Override
public String toString(){
String str="";
int i=0;
while(list[i] != null){
str += list[i]+"\n";
i++;
}
return str;
}
}
public class Driver{
public static void main(String[] args){
ObjectList list = new ObjectList();
Shape square = new Shape("square", 4);
Shape hex = new Shape("hexagon", 6);
list.add(hex);
list.add(square);
System.out.println(list);
}
}
回答by tkokasih
Indeed, you should call toString
method for each of objects that you want to print and join them together. You can use StringBuilder
to hold the string-in-the-making as follows:
实际上,您应该toString
为要打印的每个对象调用方法并将它们连接在一起。您可以使用以下StringBuilder
方式保持正在制作的字符串:
public String toString() {
StringBuilder result = new StringBuilder();
for (int i = 0; i <= numElements; i++) {
result.append(list.toString() + "\n");
}
return result.toString();
}
Note that you need to increase numElements
(e.g. numElements++
) for each add
operation as what pbabcdefp said in the comments. Also, you can use ArrayList
class to manage "growing arrays".
请注意,您需要像 pbabcdefp 在评论中所说的那样为每个操作增加numElements
(例如numElements++
)add
。此外,您可以使用ArrayList
类来管理“增长数组”。