Java 中的多个数组 toString
我试图重写 Java 中的 toString 方法,将两个(字符串)数组输出到一个字符串中,然后我可以使用换行符对其进行格式化,我假设使用 /n。我刚刚开始掌握 Java,在查看了文档之后,我仍然对这样的语法应该是什么样子感到困惑。如果有人有一个可以向我展示的例子,或者可以解释这样做的好方法,我将不胜感激。
I'm trying to override the toString method in Java to output from two arrays (of strings) into a string which I can then format with line breaks, which I assume use /n. I'm just getting to grips with Java and after looking at the documentation I'm still baffled as to how the syntax for something like this should look. If someone has an example they could show me or can explain a good method of doing this I would be most grateful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您有一个包含两个数组的类,并且想要重写
toString
方法来显示您所做的:这是一个完整的示例:
输出:
这是一个带有 new 的版本-lines:
输出:
If you have a class with two arrays, and want to override the
toString
method to show these you do:Here is a complete example:
Output:
Here is a version with new-lines:
Output:
要覆盖
toString
,您需要创建一个新的类,因为它是从Object
继承的,并且如果您创建一个内部有 2 个数组字符串的类,则它不需要两个参数那么你绝对可以做到这一点。To overwrite
toString
you need to create a new Class since it's inherited fromObject
, and it doesn't take two arguments, if you make an Class that has 2 Array Strings inside it then you could definitely do this.