The easiest way to transform collection to array?

后端 未结 8 1792
故里飘歌
故里飘歌 2020-11-27 12:37

Suppose we have a Collection. What is the best (shortest in LoC in current context) way to transform it to Foo[]? Any well-known

8条回答
  •  失恋的感觉
    2020-11-27 12:50

    For example, you have collection ArrayList with elements Student class:

    List stuList = new ArrayList();
    Student s1 = new Student("Raju");
    Student s2 = new Student("Harish");
    stuList.add(s1);
    stuList.add(s2);
    //now you can convert this collection stuList to Array like this
    Object[] stuArr = stuList.toArray();           // <----- toArray() function will convert collection to array
    

提交回复
热议问题