我发现了一些与我面临的问题相似的问题,但我无法找到解决方案。 示例:Incompatible types List of List and ArrayList of ArrayList,Not able to understand how to define a List of List in java
程序应该返回列表列表。所以,我宣布了一个列表列表,然后尝试添加arraylists。
allsubsets = new ArrayList<List<Integer>>();
但是,当我尝试从列表列表中访问每个arraylist项目时,我收到错误:incompatible types: List<Integer> cannot be converted to ArrayList<Integer>
for(ArrayList<Integer> subset:allsubsets)
当我尝试将该行转换为for(List<Integer> subset:allsubsets)
时,它会抛出添加的错误,对于List类型不存在addAll,这是有道理的。在这种情况下,请帮助我了解如何访问列表列表的元素。
public List<List<Integer>> subsets(int[] nums) {
List<Integer> arrayList = new ArrayList<Integer>();
for(int i:nums) {
arrayList.add(i);
}
return subsets(arrayList,nums.length);
}
public List<List<Integer>> subsets(List<Integer> arrayList, int index) {
List<List<Integer>> allsubsets;
if(index == -1) {
allsubsets = new ArrayList<List<Integer>>();
allsubsets.add(new ArrayList<Integer>());
}
else {
allsubsets = subsets(arrayList, index-1);
int item = arrayList.get(index);
List<List<Integer>> moresubsets = new ArrayList<List<Integer>>();
for(ArrayList<Integer> subset:allsubsets) {
//The line above throws error as I created list of lists
List<Integer> newsubset = new ArrayList<Integer>(); //create new subset
newsubset.addAll(subset); // add all old items
newsubset.add(item); // add new item
moresubsets.add(newsubset); //add subset to moresubsets
}
allsubsets.add(moresubsets); // add to actual one
}
return allsubsets;
}
注意:如果我将返回类型更改为arraylists的arraylist,它对我有用。但是,我想让它适用于列表列表
答案 0 :(得分:4)
重复列表列表的正确方法应该是:
for(List<Integer> subset:allsubsets) {
而不是:
for(ArrayList<Integer> subset:allsubsets) {
List<List<Integer>> allsubsets
被声明为列表列表,但实现未知。
只有您知道嵌套List
的类型为ArrayList
,因此要么将foreach
更改为使用List<Integer>
,要么将List<Integer>
手动转换为{{1} (这不是首选)
还有一件事:
ArrayList<>
尝试添加列表列表( allsubsets.add(moresubsets); // add to actual one
)作为应该为List<List<Integer>>
的元素,从而编译错误。
将该声明更改为:
List<Integer>
答案 1 :(得分:2)
让我们尝试将增强的for循环扩展为更基本的组件:
for(ArrayList<Integer> subset:allsubsets) {
//The line above throws error as I created list of lists
}
// this is roughly equivalent to
Iterator<List<Integer>> it = allsubsets.iterator();
while(it.hasNext()) {
ArrayList<Integer> subset = it.next(); // Error
// Since the iterator was defined as an iterator to a List<List<Integer>>,
// it.next() will return the next element in allsubsets
// which happens to be an List<Integers>.
// You can't assign a reference of a parent type to a child. However
// the opposite is perfectly fine, assigning a reference of a child type
// to a parent.
// If we change subset to be a List<Integer> i.e.
// for(List<Integer> subset : allsubsets)
// then we are assigning a reference of a List<Integer> to a List<Integer>
// so no problem.
}
答案 2 :(得分:0)
我更愿意与您分享我为管理您尝试处理的相同类型的对象列表所做的代码。希望这会有所帮助。
public static void main(String[] args) {
List<List<Integer>> allsubsets = setSubsets();
List<List<Integer>> allsubsets2 = new ArrayList<>();
allsubsets2.addAll(allsubsets);
int i= 0;
for (List<Integer> test : allsubsets2) {
System.out.println(i + " Lista");
for (Integer integer : test) {
System.out.println(integer);
}
i++;
}
}
public static List<List<Integer>> setSubsets() {
List<List<Integer>> allsubsets = new ArrayList<List<Integer>>();
List<Integer> listInteger1 = new ArrayList<>();
List<Integer> listInteger2 = new ArrayList<>();
for (int i = 0; i < 100; i++) {
listInteger1.add(i);
}
for (int i = 1010; i < 1110; i++) {
listInteger2.add(i);
}
allsubsets.add(listInteger1);
allsubsets.add(listInteger2);
return allsubsets;
}