Java: Get first item from a collection

后端 未结 12 1119
说谎
说谎 2020-11-29 17:08

If I have a collection, such as Collection strs, how can I get the first item out? I could just call an Iterator, take its first

12条回答
  •  执念已碎
    2020-11-29 17:14

    It sounds like your Collection wants to be List-like, so I'd suggest:

    List myList = new ArrayList();
    ...
    String first = myList.get(0);
    

提交回复
热议问题