【发布时间】:2022-01-06 17:09:34
【问题描述】:
您好,我想创建一个接受对象列表的方法,如下所示:
public static String formatList(List<Object> listToFormat,int indentationSize){
String indentation = Stream.generate(()->"\t").limit(indentationSize).collect(Collectors.joining());
String newIndentedLine = "\n"+indentation;
return newIndentedLine+listToFormat.stream()
.map(Object::toString)
.collect(Collectors.joining(newIndentedLine));
}
但是当我尝试做这样的事情时:
List<Car> cars = new ArrayList<>();
...
Formater.formatList(cars);
这是不允许的。
【问题讨论】:
-
您可以尝试
public static <T> String formatList(List<T> listToFormat,int indentationSize){来接受对象吗? -
它正在工作!!!谢谢!您能否将其添加为答案,以便我接受并关闭此问题。