Posted By

corporem on 10/29/11


Tagged

collectionlistarraylist


Versions (?)

Collection Sublist


 / Published in: Java
 

  1. public class CollectionUtils {
  2. public static <T> ArrayList<T> getSubList(List<T> l, int offset, int length) {
  3. if (offset >= l.size())
  4. return new ArrayList<T>(0);
  5. if (length < 0)
  6. return new ArrayList<T>(0);
  7. ArrayList<T> sl = new ArrayList<T>(length);
  8. int upper = offset + length;
  9. int bound = upper < l.size() ? upper : l.size();
  10. for (int i = offset; i < bound; i++)
  11. sl.add(l.get(i));
  12. return sl;
  13. }
  14. }

Report this snippet  

You need to login to post a comment.