How to remove the brackets [ ] from ArrayList#toString()?

后端 未结 11 2454
攒了一身酷
攒了一身酷 2021-01-03 14:09

I have created an Array List in Java that looks something like this:

 public static ArrayList error = new ArrayList<>();

for (int x= 1         


        
11条回答
  •  星月不相逢
    2021-01-03 14:42

    If you print your error list, it will internally call the toString() method of your list and this method add the brackets. There are a few possibilities. You can get the String via toString() method an remove the brackets from the String. Or you write your own method to print the List.

    public static  void printList(List list)
    {
        StringBuilder output = new StringBuilder();
        for(T element : list)
            output.append(element + ", ");
        System.out.println(output);
    }
    

提交回复
热议问题