如果我有以下列表声明:
List< Integer> b = new ArrayList< Integer>();
和
List< HashSet < Integer>> c = new ArrayList< HashSet< Integer>>();
我想知道我将如何添加b到c?
我试过c.add(b);
了,但这不起作用。
谢谢你的帮助!
如果我有以下列表声明:
List< Integer> b = new ArrayList< Integer>();
和
List< HashSet < Integer>> c = new ArrayList< HashSet< Integer>>();
我想知道我将如何添加b到c?
我试过c.add(b);
了,但这不起作用。
谢谢你的帮助!
您已声明c为集合列表(特别是“哈希集”),而不是列表列表;所以给它添加一个列表是没有意义的。如果你想c成为一个列表列表,你可以写:
List<List<Integer>> c = new ArrayList<List<Integer>>();
c.add(b);
您不能将整数添加HashSet到整数列表中。它们是两种不同的数据类型。如果您有一个HashSet整数而不是整数列表,那么您将能够将它们添加到您的列表中。
像这样:
HashSet< Integer> b = new HashSet< Integer>();
List< HashSet < Integer>> c = new ArrayList< HashSet< Integer>>();
c.add(b);
您正在使用泛型并定义b它将是一个列表,Integers并且c将是一个HashSet包含 s的列表Integers,所以不,您不能添加b到c
是的,因为第一个整数列表第二个整数哈希集列表有 ywo 变体,
像这样将第一个列表中的所有值添加到第二个列表的第一个值
HashSet<Integer> hs = new HashSet(); hs.addAll(b); c.add(hs);
for (Integer value: b){ c.add(new HashSet<Integer>(){{add(value);}};); }
您可以先创建 aHashSet并从中设置元素b,然后将 HashSet 添加到c如下:
List< Integer> b = new ArrayList< Integer>();
List< HashSet < Integer>> c = new ArrayList< HashSet< Integer>>();
HashSet < Integer> b1= new HashSet<Integer>();
b1.addAll(b);
c.add(b1);