Collection Sublist


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  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


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.