使用 toString 方法显示数组列表中的信息,无需括号和逗号
我正在使用 toString 方法来显示数组列表中的数据。如何编写代码,使其显示信息时不带 [ , , ] 围绕我的信息
请参阅下面的代码:
case 6:
System.out.println("Enter an account number to view the transactions of the account");
number=keyboard.nextLong();
found=false;
try{
for(int i=0;i<aBank.getAccounts().size();i++){
if(aBank.getAccounts().get(i).getAccountNumber().compareTo(number)==0){
found=true;
System.out.println("Account " + number + ":\tStart Balance: " +money.format(aBank.getAccounts().get(i).getStartBalance()));
System.out.println(aBank.getAccounts().get(i).getTransaction().toString());
System.out.println("Ending Balance :" +money.format(aBank.getAccounts().get(i).getBalance()));
}
}
}
catch (Exception e){
System.out.println("Unable to process request.\n" +e.getMessage());
}
break;
这是输出:
Account 1: Start Balance: $1000.00
[Deposit 1 6/6/2011 $500.00
, Deposit 2 6/6/2011 $489.00
, Deposit 3 6/6/2011 $262.00
, Withdrawal 4 6/6/2011 $897.00
, Withdrawal 5 6/6/2011 $56.32
, Withdrawal 6 6/6/2011 $78.24
]
Ending Balance :$1219.44
注意需要删除的括号和逗号
I am using a toString method to display data in an arraylist. How do I write the code so that it displays the information without the [ , , ] around my information
see the code below:
case 6:
System.out.println("Enter an account number to view the transactions of the account");
number=keyboard.nextLong();
found=false;
try{
for(int i=0;i<aBank.getAccounts().size();i++){
if(aBank.getAccounts().get(i).getAccountNumber().compareTo(number)==0){
found=true;
System.out.println("Account " + number + ":\tStart Balance: " +money.format(aBank.getAccounts().get(i).getStartBalance()));
System.out.println(aBank.getAccounts().get(i).getTransaction().toString());
System.out.println("Ending Balance :" +money.format(aBank.getAccounts().get(i).getBalance()));
}
}
}
catch (Exception e){
System.out.println("Unable to process request.\n" +e.getMessage());
}
break;
this is the output:
Account 1: Start Balance: $1000.00
[Deposit 1 6/6/2011 $500.00
, Deposit 2 6/6/2011 $489.00
, Deposit 3 6/6/2011 $262.00
, Withdrawal 4 6/6/2011 $897.00
, Withdrawal 5 6/6/2011 $56.32
, Withdrawal 6 6/6/2011 $78.24
]
Ending Balance :$1219.44
notice the brackets and commas that need to be removed
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这又如何呢?
What about this?
尝试写得更具可读性。
Try to write it more readable.
根本不要使用 toString()。它不适合用于创建格式化的 UI 字符串。它是一个开发人员工具,而不是一个用户演示工具。
对于对象表示具有简单可接受格式的东西的情况,我将添加一个警告,该格式可以满足这两个目的。 Java 原始包装器(如 Integer 和 Float)就是一个例子。其他简单的类(例如 2D 点)也可以在这方面发挥作用,但随着对象变得更加复杂,这种方法很快就会崩溃。
要执行您正在尝试的操作,只需创建一个方法来进行显示并在该方法中进行格式化即可。
Don't use toString() at all. It is not meant to be used to create formatted UI strings. It is a developer tool, not a user presentation tool.
I will add a caveat for the cases where the object represents something with a simple accepted format that can serve both purposes. Java primitive wrappers (like Integer and Float) are an example of this. Other simple classes such as a 2D point can work in this regard as well, but this quickly falls apart as an object becomes more complex.
To do what you are attempting, just create a method to do your display and do the formatting in that method instead.
使用 番石榴加入者 如
Use a guava joiner as in
您真的需要使用 toString() 方法吗?
否则,您可以自己为每个数组构建字符串。
Do you really need to use the toString() Method?
Otherwise you could build up the string for each array by yourself.
修改“getTransaction()”返回的对象的“toString”方法!?
Modify the "toString" method of the Object returned by "getTransaction()" !?