java 使用 toString 方法在不带括号和逗号的数组列表中显示信息

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6252973/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 15:03:04  来源:igfitidea点击:

using the toString method to display information in an arraylist without brackets and commas

javaarraylisttostring

提问by trs

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

我正在使用 toString 方法在数组列表中显示数据。我如何编写代码,以便在我的信息周围没有 [ , , ] 的情况下显示信息

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: 00.00
[Deposit    1   6/6/2011    0.00
, Deposit   2   6/6/2011    9.00
, Deposit   3   6/6/2011    2.00
, Withdrawal    4   6/6/2011    7.00
, Withdrawal    5   6/6/2011    .32
, Withdrawal    6   6/6/2011    .24
]
Ending Balance :19.44

notice the brackets and commas that need to be removed

注意需要删除的括号和逗号

回答by Robin

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.

根本不要使用 toString() 。它不打算用于创建格式化的 UI 字符串。它是一个开发者工具,而不是一个用户展示工具。

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.

对于对象表示具有可用于这两个目的的简单可接受格式的内容的情况,我将添加一个警告。Java 原始包装器(如 Integer 和 Float)就是一个例子。其他简单的类(例如 2D 点)也可以在这方面起作用,但是随着对象变得更加复杂,这种方法很快就会崩溃。

To do what you are attempting, just create a method to do your display and do the formatting in that method instead.

要执行您正在尝试的操作,只需创建一个方法来进行显示并在该方法中进行格式化。

回答by Mike Samuel

Use a guava joineras in

使用番石榴木匠

Joiner.on("").join(
    aBank.getAccounts().get(i).getTransaction())

回答by AlfonsSocken

Do you really need to use the toString() Method? Otherwise you could build up the string for each array by yourself.

你真的需要使用 toString() 方法吗?否则,您可以自己为每个数组构建字符串。

tmpArray = aBank.getAccounts().get(i).getTransaction();
StringBuilder builder = new StringBuilder();
for (String value : tmpArray) {
  builder.append(value);
}    
System.out.println(builder.toString());

回答by Patryk Dobrowolski

Try to write it more readable.

尽量写得更易读。

   try {
    for(BankAccount ba : aBank.getAccounts()) {
      if(ba.getAccountNumber().equals(number)) {
        found=true;
        System.out.println("Account " + number + ":\tStart Balance: " 
         +money.format(ba.getStartBalance()));                  
        for (Object tr : ba.getTransaction()) {
          System.out.println(tr);
        }
        System.out.println("Ending Balance :" +money.format(ba.getBalance()));
       }
      }
}

回答by Arne Deutsch

Modify the "toString" method of the Object returned by "getTransaction()" !?

修改“getTransaction()”返回的Object的“toString”方法!?

回答by Bohemian

What about this?

那这个呢?

List<String> list = ArrayList<String>();
String asString = list.toString().replaceAll("^\[", "").replaceAll("\]$", "").replace(",", "");