Java examples for java.util:List Element
Copies all of the elements from one list into another.
//package com.java2s; import java.util.List; import java.util.ListIterator; import java.util.RandomAccess; public class Main { public static void main(String[] argv) { List dest = java.util.Arrays.asList("asdf", "java2s.com"); List src = java.util.Arrays.asList("asdf", "java2s.com"); System.out.println(copy(dest, src)); }//www. ja v a2 s .c o m /** * */ private static final int COPY_THRESHOLD = 10; /** * Copies all of the elements from one list into another. After the * operation, the index of each copied element in the destination list will * be identical to its index in the source list. The destination list must * be at least as long as the source list. If it is longer, the remaining * elements in the destination list are unaffected. * <p> * This method runs in linear time. * @param <T> . * @param dest The destination list. * @param src The source list. * * @return boolean isCopyValide */ public static <T> Boolean copy(List<? super T> dest, List<? extends T> src) { Boolean isCopyValide = null; int srcSize = src.size(); if (srcSize > dest.size()) { isCopyValide = false; throw new IndexOutOfBoundsException( "Source does not fit in dest"); } if (srcSize < COPY_THRESHOLD || (src instanceof RandomAccess && dest instanceof RandomAccess)) { for (int i = 0; i < srcSize; i++) { dest.set(i, src.get(i)); } } else { ListIterator<? super T> di = dest.listIterator(); ListIterator<? extends T> si = src.listIterator(); for (int i = 0; i < srcSize; i++) { di.next(); di.set(si.next()); } } isCopyValide = true; return isCopyValide; } }