通过toString方法来显示不带括号和逗号在一个ArrayList信息 [英] using the toString method to display information in an arraylist without brackets and commas
问题描述
我使用的是toString方法在一个ArrayList中显示的数据。我怎样写的code,使其不显示的信息[,]在我的信息
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
看到code以下:
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
注意,需要删除括号和逗号
notice the brackets and commas that need to be removed
推荐答案
在所有不使用的toString()。它不意味着被用来创建格式化的用户界面的字符串。这是一个开发工具,而不是用户presentation工具。
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.
我会加警告的情况下重新对象presents用的东西,可以同时服务于目的的简单接受的格式。 Java原始包装(如整数和浮点型)是这样的一个例子。其他简单的类,如2D点可以在这方面的工作,以及,但这很快分崩离析随着一个物体变得更加复杂。
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.
这篇关于通过toString方法来显示不带括号和逗号在一个ArrayList信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!