Here you can find the source of add(List extends E> list, E element)
Parameter | Description |
---|---|
list | a parameter |
element | a parameter |
public static <E> List<E> add(List<? extends E> list, E element)
//package com.java2s; /******************************************************************************* * Copyright 2011 Danny Kunz/* w w w.j a v a 2 s.co m*/ * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. ******************************************************************************/ import java.util.ArrayList; import java.util.List; public class Main { /** * Returns the given {@link List} instance or a new {@link List} instance if the given one is null. The returned instance will * contain all elements of the given {@link List} and additionally the further given element. <br> * <br> * This function will return always an instance, even if the given list is null. * * @param list * @param element * @return given {@link List} instance or new {@link List} instance if given {@link List} instance is null */ public static <E> List<E> add(List<? extends E> list, E element) { // @SuppressWarnings("unchecked") List<E> retlist = (List<E>) list; // if (list == null) { retlist = new ArrayList<E>(); } // retlist.add(element); // return retlist; } /** * Similar to {@link #add(List, Object)} allowing to specify an index position * * @param list * @param index * @param element * @return the given {@link List} instance or a new one */ public static <E> List<E> add(List<? extends E> list, int index, E element) { // @SuppressWarnings("unchecked") List<E> retlist = (List<E>) list; // if (list == null) { retlist = new ArrayList<E>(); } // while (list.size() < index) { list.add(null); } // retlist.add(index, element); // return retlist; } /** * Returns the given {@link List} instance or a new {@link List} instance if the given one is null. The returned instance will * contain all elements of the given {@link List} and additionally all further given elements. <br> * <br> * This function will return always a instance, even if the given list is null. * * @param list * @param elements * @return given {@link List} instance or new {@link List} instance if given {@link List} instance is null */ public static <E> List<E> add(List<? extends E> list, E... elements) { // @SuppressWarnings("unchecked") List<E> retlist = (List<E>) list; // if (list == null) { retlist = new ArrayList<E>(); } // if (elements != null) { for (E element : elements) { retlist.add(element); } } // return retlist; } /** * Similar to {@link #add(List, Object...)} allowing to specify an index position * * @param list * @param index * @param elements * @return the given {@link List} instance or a new one */ public static <E> List<E> add(List<? extends E> list, int index, E... elements) { // @SuppressWarnings("unchecked") List<E> retlist = (List<E>) list; // if (list == null) { retlist = new ArrayList<E>(); } // while (list.size() < index) { list.add(null); } // int relative = 0; if (elements != null) { for (E element : elements) { retlist.add(index + relative, element); relative++; } } // return retlist; } }