且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我如何可以覆盖在Java中一个ArrayList的toString方法?

更新时间:2023-02-18 16:07:24

您应该做这样的事情。

public static String listToString(List<?> list) {
    String result = "+";
    for (int i = 0; i < list.size(); i++) {
        result += " " + list.get(i);
    }
    return result;
}

和传递名单 listToString的参数()。您的可以的技术上延长的ArrayList (无论是与一个匿名类或具体的)和实施的toString 自己,但似乎没有必要在这里。

and pass the list in as an argument of listToString(). You can technically extend ArrayList (either with an anonymous class or a concrete one) and implement toString yourself, but that seems unnecessary here.