如何快速方便地創建一個單元素數組列表 [重複] - How to quickly and conveniently create a one element arraylist [duplicate]

問題:

This question already has answers here : 這個問題在這裏已經有了答案
Closed 4 years ago . 4年前關閉。

Is there a Utility method somewhere that can do this in 1 line?是否有可以在 1 行中執行此操作的實用程序方法? I can't find it anywhere in Collections , or List .我在CollectionsList任何地方都找不到它。

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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章