我們通過列表和子列表來查找列表。 我想在列表中刪除列表中的第一個子列表。將元素從子列表中移除到列表中
這裏是我想要的一個例子:
removeSubList([3,2,3,4,5,3],[3]) [2,3,4,5,3]
removeSubList([2,3,4,5,3,4],[3,4]) [2,5,3,4]
removeSubList([3,2,3,4,7],[3,7]) [2,3,4]
removeSubList([3,2,3,4,5,3],[]) [3,2,3,4,5,3]
removeSubList([],[3,7]) []
removeSubList(null,[3,7]) null
removeSubList([3,2,3,4,5,3],null) [3,2,3,4,5,3]
這裏是我的代碼我試過,但它不工作...
public class RemovePositionList<E> {
/**
* Returns in "list" the previous content of "list" after removing "subList" if it is present in "list"
*
* @param list (input/output) The list where search and remove the first occurrence of "subList"
* @param subList (input) The sub list to search within "list"
*/
public void removeSubList(PositionList<E> list,PositionList<E> subList) {
Position<E> cursor = list.first();
Position<E> cursor2 = subList.first();
while(cursor != null && cursor2 != null){ //comprobamos que ningun elemento es null
if(cursor.element().equals(cursor2)){
list.remove(cursor);
}
else{
list.next(cursor);
list.next(cursor2);
}
}
}
}
感謝您的幫助...
什麼位置和PositionList?你在寫自己的迭代器嗎? – Joel