本文介绍了Collections.nCopies没有创造名单的副本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建一个列表的列表用默认大小集。所以,我现在用的设置方法添加数据的列表。

我的code:

 封装测试;进口的java.util.ArrayList;
进口java.util.Collections中;
进口的java.util.List;公共类的Test2 {
公共静态无效的主要(字符串ARGS []){
    清单<名单,LT;弦乐>>键=新的ArrayList<>(Collections.nCopies(3,新的ArrayList<>()));
    清单<串GT;值= keys.get(2);
    values​​.add(你好);
    values​​.add(二合一);
    keys.set(2,值);
    清单<串GT; tempList = keys.get(1);
    tempList.add(现在在加1);
    keys.set(1,tempList);
    的System.out.println(位置1+ keys.get(1)的ToString());
    的System.out.println(位置2+ keys.get(2)的ToString());
}
}

我的输出:

 位置1 [你好,一,二,现在,在1加]
位置2 [你好,一,二,现在,在1加]

为什么这样做呢?第一个数据获取的追加到第二个呢?我在做什么错了?


解决方案

 列表<名单,LT;弦乐>>键=新的ArrayList<>(Collections.nCopies(3,新的ArrayList<>()))

在此声明它没有创造三种新实例的ArrayList<> 。它创建它的有一个的实例,并创建一个具有三个引用到的相同的实例的列表。因此,在名单列表中的每个位置是相同的的ArrayList 实例的引用。

您可以做来解决这个问题最简单的办法是使用循环:

 的for(int i = 0; I< numCopies;我++){
    keys.add(新的ArrayList<>());
}

使用Java 8的流API一个等效的解决方案:

  IntStream.range(0,N).mapToObj(ArrayList的<串GT; ::新).forEach(键::补充);

或者你可以一气呵成创建像这样:

 列表<名单,LT;弦乐>>键= IntStream.range(0,numCopies)
                                   .mapToObj(ArrayList的<串GT; ::新)
                                   .collect(Collectors.toList());

I have created a List of Lists with a default size set. Therefore, I am adding the data to the lists using the set method.

My Code:

package test;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Test2 {
public static void main(String args[]) {
    List<List<String>> keys = new ArrayList<>(Collections.nCopies(3, new ArrayList<>()));
    List<String> values = keys.get(2);
    values.add("Hello");
    values.add("One Two");
    keys.set(2, values);
    List<String> tempList = keys.get(1);
    tempList.add("Now adding at 1");
    keys.set(1, tempList);
    System.out.println("Position 1 " + keys.get(1).toString());
    System.out.println("Position 2 " + keys.get(2).toString());
}
}

My Output:

Position 1 [Hello, One Two, Now adding at 1]
Position 2 [Hello, One Two, Now adding at 1]

Why is it doing this? The first data get's appended onto the second as well? What am I doing wrong ?

解决方案
List<List<String>> keys = new ArrayList<>(Collections.nCopies(3, new ArrayList<>()))

In this statement it's not creating three new instances of ArrayList<>. It creates one instance of it and creates a list with three references to the same instance. Hence each location in the list of lists is a reference to the same ArrayList instance.

The simplest thing you can do to get around this is to use a for loop:

for(int i = 0; i < numCopies; i++) {
    keys.add(new ArrayList<>());
}

An equivalent solution using Java 8's streaming API:

IntStream.range(0, n).mapToObj(ArrayList<String>::new).forEach(keys::add);

Or you can create keys in one go like so:

List<List<String>> keys = IntStream.range(0, numCopies)
                                   .mapToObj(ArrayList<String>::new)
                                   .collect(Collectors.toList());

这篇关于Collections.nCopies没有创造名单的副本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 15:12