Here you can find the source of slice(List
Parameter | Description |
---|---|
c | a parameter |
fromIndex | a parameter |
toIndex | a parameter |
public static <T> List<T> slice(List<T> c, int fromIndex, int toIndex)
//package com.java2s; /******************************************************************************* * Copyright (c) 2010-2015 BSI Business Systems Integration AG. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors:// w w w.j a v a2 s . c om * BSI Business Systems Integration AG - initial API and implementation ******************************************************************************/ import java.util.ArrayList; import java.util.Collection; import java.util.List; public class Main { /** * Returns a sub list with the provided indices. This methods supports negative indices in the sense that -1 addresses * the last element and -2 the 2nd last. For positive indices, the methods uses * {@link java.util.List#subList(int, int)} with one change: It is returning the sub list starting with c[fromIndex] * to c[toIndex]. slice(c, 0, 0): first element of c slice(c, 0, -3): c without the last two elements * * @param c * @param fromIndex * @param toIndex * @return */ public static <T> List<T> slice(List<T> c, int fromIndex, int toIndex) { List<T> result = new ArrayList<T>(); // null check if (c == null) { return result; } int len = c.size(); // arguments check if (fromIndex > len || toIndex > len || fromIndex < -len || toIndex < -len) { throw new IndexOutOfBoundsException("fromIndex or toIndex out of bounds"); } // special case for empty list if (len > 0 && fromIndex >= len) { throw new IndexOutOfBoundsException("fromIndex or toIndex out of bounds"); } // map negative indices if (fromIndex < 0) { fromIndex += len; } if (toIndex < 0) { toIndex += len + 1; } else if (toIndex == 0 && len > 0 || toIndex > 0) { toIndex++; } return new ArrayList<T>(c.subList(fromIndex, toIndex)); } public static <T> int size(Collection<T> list) { if (list == null) { return 0; } return list.size(); } }