简体   繁体   English

在Java中打印对象的二维数组

[英]printing a 2 dimensional array of objects in java

I have a 2-d array which contains objects.does anyone knows how can i print this array?Because when i use the following code doesn't print the contain of the object but print symbols. 我有一个包含对象的二维数组。有人知道如何打印此数组吗?因为当我使用以下代码时,不会打印对象的包含内容,但会打印符号。

Humans [][]cells= new Humans [7][7];

for (int row=0; row<cells.length; row++) {
      for (int col=0; col<cells[row].length; col++) {


            System.out.print("  " + cells[row][col]);


           } 
} 

You need to add a toString() to your Humans object if you want to include it in string concatenation. 如果要将toString()包含在字符串连接中,则需要将其添加到Humans对象中。

public String toString() {
    return "Name: " + name + " age: " + age + " shoe size: " + shoeSize;
}

you need to use toString , and make sure the instances in the 2d array implement it appropriately. 您需要使用toString ,并确保2d数组中的实例正确实现了它。

In your inner most loop something like 在你最内层的循环中

Human current = cells[row][col]; 
// possibly do a null check
System.out.print(current.toString());

Be sure to implement toString on Human . 确保在Human上实现toString

You need to override the method toString in your Human class to print any details of the class you want (I'm assuming name and stuff) and then instead of : 您需要在Human类中重写toString方法以显示所需类的任何详细信息(我假设是名称和内容),然后代替:

cells[row][col]

you should write : 你应该写:

cells[row][col].toString()

As @hvgotcodes has said you need to ensure that your Human class has implemented the toString method. 正如@hvgotcodes所说,您需要确保您的Human类已实现toString方法。 For example if a Human just contains a single number (the person's age), the most basic it might look like this: 例如,如果一个人仅包含一个数字(该人的年龄),则最基本的数字可能是这样的:

class Human {
    int age;
    public Human(int age){
        this.age = age;
    }       
    @Override
    public String toString(){
        return "[Human: age :=" + age + "]"; //or whatever's appropriate!
    }
}

To then print your 2d array of these objects you can write: 要打印这些对象的二维数组,可以编写:

for(int i = 0; i < cells.length; i++){
    for(int j = 0; j < cells[i].length; j++){
        System.out.print(cells[i][j]); //invokes toString method in Human..
        if(j < cells[i].length -1)
            System.out.print(", ");
        }
        System.out.println();
    }
}

Override the toString() method in your humans class and print what you want to be printed for a human. 在您的humans类中重写toString()方法,并打印要为人类打印的内容。 Then use 然后使用

Arrays.toString(cells);

to print your array. 打印数组。

Can you override the Humans toString()? 您可以覆盖人类toString()吗?

Edit: Here is good reference http://www.javapractices.com/topic/TopicAction.do?Id=55 编辑:这是很好的参考http://www.javapractices.com/topic/TopicAction.do?Id=55

您需要重写Human类中的toString以满足您的需求。

First, you need to override the toString() method of the Humans object to return a String that represents the contents of an instance of Humans. 首先,您需要重写Humans对象的toString()方法,以返回表示Humans实例内容的String。

Next, you'll probably want to print a new line after the inner for loop completes, otherwise you're going to print the entire contents of the entire 2D array on just one line (unless that's what you're going for here). 接下来,您可能要在内部for循环完成之后打印新行,否则,您将仅在一行上打印整个2D数组的全部内容(除非您要在此处进行此操作)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM