需要将 List<E> 转换为 Element

Need to convert List<E> to an Element

我正在传入两个元素 E 列表,并希望将其中一个列表插入到另一个列表中的指定索引处。我收到错误 "List cannot be converted to E" 并且不确定如何转换元素。我是否应该尝试不同于 .add() 的方法,或者是否可以转换元素以便它可以与 .add() 方法一起使用?

我需要 return 一个用于调用此方法的其他方法的列表。

编辑: 我正在传递一个整数数组,例如 list=[1,2,3] 我想将 c=[0] 放在位置 i =2 给出 [1,2,0,3]

Edit2: 正如古斯塔沃指出的那样,我需要做的就是为元素位置添加 c.get(0)。

public List<E> next(List<E> list,List<E> c, int index){
    list.add(index,c);
    return list;
}

I am passing in two lists of element E and want to insert one of the lists into the other at a specified index

你的问题不清楚:

是否要将列表 c 中位于 index 的项目插入到 list 中?

list.add(c.get(index));

是否要将列表 c 中位于 index 的项目插入到位于 indexlist 中?

list.add(index, c.get(index));

根据您对问题的版本:

I am passing in an array of ints so for example list=[1,2,3] and I want to place c=[0] at location i=2 giving [1,2,0,3]

list.add(index, c.get(0));

在索引 i 处将列表 E 分成两部分,然后按顺序将所有 3 个列表连接在一起:E_part1 + c + E_part2

您不能将 List 转换为元素 E - 您尝试添加的方式将意味着这种不可能的转换。