問題:
Is there a Utility method somewhere that can do this in 1 line?是否有可以在 1 行中執行此操作的實用程序方法? I can't find it anywhere in Collections
, or List
.我在Collections
或List
任何地方都找不到它。
public List<String> stringToOneElementList(String s) {
List<String> list = new ArrayList<String>();
list.add(s);
return list;
}
I don't want to re-invent the wheel unless I plan on putting fancy rims on it.我不想重新發明輪子,除非我打算在上面裝上花哨的輪輞。
Well... the type can be T
, and not String
.嗯...類型可以是T
,而不是String
。 but you get the point.但你明白了。 (with all the null checking, safety checks...etc) (所有的空檢查,安全檢查......等)
解決方案:
參考一: https://en.stackoom.com/question/1NQH5參考二: https://stackoom.com/question/1NQH5