Java List Truncate setSize(L list, int newsize)

Here you can find the source of setSize(L list, int newsize)

Description

Truncates 'list' or adds null elements to its end

License

Open Source License

Declaration

public static <T, L extends java.util.List<T>> L setSize(L list, int newsize) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (C) 2006-2013 AITIA International, Inc.
 * /*from  w  ww.j av a  2  s. c  o m*/
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 ******************************************************************************/

public class Main {
    /** Truncates 'list' or adds null elements to its end */
    public static <T, L extends java.util.List<T>> L setSize(L list, int newsize) {
        int n = list.size();
        if (newsize > n) {
            list.addAll(java.util.Collections.<T>nCopies(newsize - n, null));
        } else if (newsize < n) {
            list.subList(newsize, n).clear();
        }
        return list;
    }

    /** Removes all content from 'sb'. */
    public static void clear(StringBuilder sb) {
        if (sb != null && sb.length() > 0)
            sb.delete(0, sb.length());
    }
}

Related

  1. setSize(List list, int size)
  2. trunc(List as, int length)
  3. truncate(final List list, final int newSize)
  4. truncate(final List items, final int limit)