Copying the first half of an ArrayList

前端 未结 4 1988
野性不改
野性不改 2021-01-14 08:53

There is an ArrayList al, and I want to copy the first half of its elements into another ArrayList firstHalf. (If al

4条回答
  •  终归单人心
    2021-01-14 09:02

    List firstHalf = al.subList(0, (int) al.size() / 2 + 1);
    

    Is easier convert the al.size() / 2 to integer than doing "%2".

    You add 1 to firstHalf to be bigger than the second half.

提交回复
热议问题