java.util.Collections.copy()方法的使用及代码示例

x33g5p2x  于2022-01-16 转载在 其他  
字(6.3k)|赞(0)|评价(0)|浏览(136)

本文整理了Java中java.util.Collections.copy()方法的一些代码示例,展示了Collections.copy()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Collections.copy()方法的具体详情如下:
包路径:java.util.Collections
类名称:Collections
方法名:copy

Collections.copy介绍

[英]Copies the elements from the source list to the destination list. At the end both lists will have the same objects at the same index. If the destination array is larger than the source list, the elements in the destination list with index >= source.size() will be unchanged.
[中]

代码示例

代码示例来源:origin: stackoverflow.com

// copy(dest, src)
Collections.copy(new ArrayList<Number>(), new ArrayList<Number());
Collections.copy(new ArrayList<Number>(), new ArrayList<Integer());
Collections.copy(new ArrayList<Object>(), new ArrayList<Number>());
Collections.copy(new ArrayList<Object>(), new ArrayList<Double());

代码示例来源:origin: stackoverflow.com

// note: instantiating with a.size() gives `b` enough capacity to hold everything
List<String> b = new ArrayList<String>(a.size());
Collections.copy(b, a);

代码示例来源:origin: firebase/firebase-jobdispatcher-android

static List<List<Integer>> getAllConstraintCombinations() {
 List<List<Integer>> combos = new ArrayList<>();
 combos.add(Collections.<Integer>emptyList());
 for (Integer cur : Constraint.ALL_CONSTRAINTS) {
  for (int l = combos.size() - 1; l >= 0; l--) {
   List<Integer> oldCombo = combos.get(l);
   List<Integer> newCombo = Arrays.asList(new Integer[oldCombo.size() + 1]);
   Collections.copy(newCombo, oldCombo);
   newCombo.set(oldCombo.size(), cur);
   combos.add(newCombo);
  }
  combos.add(Collections.singletonList(cur));
 }
 return combos;
}

代码示例来源:origin: stackoverflow.com

//assume oldList exists and has data in it.
List<String> newList = new ArrayList<String>();
Collections.copy(newList, oldList);

代码示例来源:origin: stackoverflow.com

ArrayList copy = new ArrayList (original.size());
Collections.copy(copy, original);

代码示例来源:origin: stackoverflow.com

for(ArrayList<Arc> list : rotalar1)
 {
   ArrayList<Arc> copy = new ArrayList<>();
   Collections.copy(copy, list);  //or copy.addAll(list);
   rotalar2.add(copy);
 }

代码示例来源:origin: stackoverflow.com

List<Flight> a = new ArrayList<>();
 List<Flight> copy = new ArrayList<>();
 Collections.copy(copy, a);
 Iterator<Flight> it = a.iterator();
 while(it.hasNext()){
   Flight f = it.next();
   if(shouldGetRemoved(f, copy)){
     it.remove();
   }
 }

代码示例来源:origin: org.glassfish.main.security/security

private <U extends BaseAuditModule> List<U> copyAndAdd(final List<U> orig, final U am) {
  final List<U> list = new ArrayList<U>();
  Collections.copy(orig, list);
  list.add(am);
  return list;
}

代码示例来源:origin: stackoverflow.com

public static <T extends Object> List<T> reverse(List<T> list) {
  List<T> copy = Collections.emptyList();
  Collections.copy(copy, list);
  Collections.reverse(copy);
  return copy;
}

代码示例来源:origin: org.glassfish.main.security/security

private <U extends BaseAuditModule> List<U> copyAndRemove(final List<U> orig, final U am) {
  final List<U> list = new ArrayList<U>();
  Collections.copy(orig, list);
  list.remove(am);
  return list;
}

代码示例来源:origin: stackoverflow.com

List<String> originalList = Arrays.asList(ARRAY_ARGS);
List<String> list = new ArrayList<String>(originalList);
Collections.copy(list, originalList);
list.add(additionalArgument);
foo(list.toArray(new String[list.size()]));

代码示例来源:origin: nifty-gui/nifty-gui

public ControlEffectAttributes(
  @Nonnull final Attributes attributes,
  @Nonnull final List<EffectValueType> effectValues) {
 this.attributes = new Attributes(attributes);
 this.effectValues = new ArrayList<EffectValueType>(effectValues);
 Collections.copy(this.effectValues, effectValues);
}

代码示例来源:origin: jobxhub/JobX

public static List<Field> getAllFields(Class<?> objClass) {
  AssertUtils.notNull(objClass);
  List<Field> fields = new ArrayList<Field>(0);
  while (!objClass.getSuperclass().equals(Object.class)) {
    Collections.copy(fields, Arrays.asList(objClass.getDeclaredFields()));
    objClass = objClass.getSuperclass();
  }
  return fields;
}

代码示例来源:origin: EvoSuite/evosuite

private Stack<MethodCall> copyCallStack(Stack<MethodCall> callStack) {
  Stack<MethodCall> r = new Stack<MethodCall>();
  r.setSize(callStack.size());
  Collections.copy(r, callStack);
  return r;
}

代码示例来源:origin: stackoverflow.com

public List<RestaurantData> sort(RArrayList<RestaurantData> datas) {
 // Create a list with enough capacity for all elements
 List<RestaurantData> newList = new RArrayList<RestaurantData>(datas.size());
 Collections.copy(newList, datas);
 Collections.sort(newList, new Comparator<RestaurantData>() {
    @Override
    public int compare(RestaurantData lhs, RestaurantData rhs) {
      return lhs.getDistance() - rhs.getDistance();
    }
  });
 return newList;
}

代码示例来源:origin: nifty-gui/nifty-gui

public ControlEffectOnHoverAttributes(
  @Nonnull final Attributes attributes,
  @Nonnull final List<EffectValueType> effectValues,
  @Nonnull final HoverType hoverType) {
 this.attributes = new Attributes(attributes);
 this.effectValues = new ArrayList<EffectValueType>(effectValues);
 Collections.copy(this.effectValues, effectValues);
 this.controlHoverAttributes = new ControlHoverAttributes(hoverType);
}

代码示例来源:origin: stackoverflow.com

List<? extends Fruit> basket = new ArrayList<? extends Fruit>();
basket.add(new Apple());
basket.add(new Pear());
basket.add(new Tomato());
List<Fruit> fridge = new ArrayList<Fruit>(); 

Collections.copy(fridge, basket);// works

代码示例来源:origin: stackoverflow.com

List<Apple> basket = new ArrayList<Apple>();
basket.add(new Apple());
basket.add(new Apple());
basket.add(new Apple());
List<Fruit> fridge = new ArrayList<Fruit>();

Collections.copy(fridge, basket); /* works since the basket is defined as a List of apples and not a list of some fruits. */

代码示例来源:origin: org.reactfx/reactfx

default MaterializedListChange<E> materialize() {
  List<E> added = new ArrayList<>(getAddedSize());
  Collections.copy(added, getAddedSublist());
  added = Collections.unmodifiableList(added);
  return new MaterializedListChangeImpl<>(getFrom(), getRemoved(), added);
}

代码示例来源:origin: lorddusk/HQM

private void countItems(EntityPlayer player, ItemStack stack) {
  if(!player.getEntityWorld().isRemote){
    NonNullList<ItemStack> items = NonNullList.withSize(player.inventory.mainInventory.size() + 1, ItemStack.EMPTY);
    Collections.copy(items, player.inventory.mainInventory);
    if(!stack.isEmpty()){
      items.set(items.size() - 1, stack);
    }
    countItems(items, (QuestDataTaskItems) getData(player), player.getPersistentID());
  }
}

相关文章

微信公众号

最新文章

更多

Collections类方法