arraylist切割_Java:如何在多个小ArrayList中分割ArrayList?

  • Post author:
  • Post category:java


您可以使用

subList(int fromIndex, int toIndex)获取原始列表的一部分的视图。

从API:

Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations supported by this list.

例:

List numbers = new ArrayList(

Arrays.asList(5,3,1,2,9,5,0,7)

);

List head = numbers.subList(0, 4);

List tail = numbers.subList(4, 8);

System.out.println(head); // prints “[5, 3, 1, 2]”

System.out.println(tail); // prints “[9, 5, 0, 7]”

Collections.sort(head);

System.out.println(numbers); // prints “[1, 2, 3, 5, 9, 5, 0, 7]”

t



版权声明:本文为weixin_39614011原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。