在Java中检索递归调用之间的值
我有一个递归代码,可以在每次调用时从集合中删除值。当它从调用返回到上一个递归时,我希望该集合以与进入调用之前完全相同的状态进行补充。因此,对于以下代码中的eg:在Java中检索递归调用之间的值,java,recursion,Java,Recursion,我有一个递归代码,可以在每次调用时从集合中删除值。当它从调用返回到上一个递归时,我希望该集合以与进入调用之前完全相同的状态进行补充。因此,对于以下代码中的eg: // initial value of this list is [a,b,c] void foo(ArrayList<Character> myList) { for(int i=0; i< size;i++) { myList.remove(i); // Now it become
// initial value of this list is [a,b,c]
void foo(ArrayList<Character> myList)
{
for(int i=0; i< size;i++)
{
myList.remove(i); // Now it becomes [b,c]
foo(myList);
/* QUESTION: at this point how do i retrieve the value [b,c] -- because it goes into successive recursive calls I'm unable to get this value back!!*/
}
}
//此列表的初始值为[a,b,c]
void foo(ArrayList myList)
{
对于(int i=0;i
在递归调用中将myList
的副本传递给foo:
// initial value of this list is [a,b,c]
@SuppressWarnings("unchecked")
static void foo(ArrayList<Character> myList)
{
for(int i=0; i< myList.size();i++)
{
myList.remove(i); // Now it becomes [b,c]
foo((ArrayList<Character>)myList.clone());
}
}
void foo(ArrayList<Character> myList)
{
for(int i=0; i< size;i++)
{
myList.remove(i); // Now it becomes [b,c]
ArrayList<Character> lastList = new ArrayList<Character>(myList);
foo(myList);
/* QUESTION: at this point how do i retrieve the value [b,c] -- because it goes into successive recursive calls I'm unable to get this value back!!*/
// Answer: you now have a copy of the list prior to the recursive call in lastList
}
}
//此列表的初始值为[a,b,c]
@抑制警告(“未选中”)
静态void foo(ArrayList myList)
{
对于(int i=0;i
一般来说,你不需要
要保留某些内容,需要在递归调用之前对其进行复制:
// initial value of this list is [a,b,c]
@SuppressWarnings("unchecked")
static void foo(ArrayList<Character> myList)
{
for(int i=0; i< myList.size();i++)
{
myList.remove(i); // Now it becomes [b,c]
foo((ArrayList<Character>)myList.clone());
}
}
void foo(ArrayList<Character> myList)
{
for(int i=0; i< size;i++)
{
myList.remove(i); // Now it becomes [b,c]
ArrayList<Character> lastList = new ArrayList<Character>(myList);
foo(myList);
/* QUESTION: at this point how do i retrieve the value [b,c] -- because it goes into successive recursive calls I'm unable to get this value back!!*/
// Answer: you now have a copy of the list prior to the recursive call in lastList
}
}
void foo(ArrayList myList)
{
对于(int i=0;i
这是我递归执行的方式:
void foo(List<Character> list){
if(list.size()>0){ //recursion condition
list.remove(0);
foo(list);
}
}
void foo(列表){
if(list.size()>0){//递归条件
列表。删除(0);
foo(名单);
}
}
但是您似乎对在任何递归之前访问原始列表感兴趣。因此,您将被迫保持原始参数列表不变,并将其与包含原始输入的计算或处理进度的另一个列表一起传递给每个递归
List<Character> foo(List<Character> source, List<Character> result){
if(result == null){ // only on first call
result = new ArrayList<Character>(source); //accumulator copy
}
//here you could do any comparisons you want
if(!result.isEmpty()){ //recursion condition
result.remove(0);
return foo(source, result);
}
return result;
}
listfoo(列表源、列表结果){
如果(result==null){//仅在第一次调用时
结果=新建ArrayList(源);//累加器副本
}
//这里你可以做任何你想要的比较
如果(!result.isEmpty()){//递归条件
结果:删除(0);
返回foo(来源、结果);
}
返回结果;
}
这就是我使用它的方式
List<Character> letters = Arrays.asList('a', 'b', 'c');
List<Character> result = foo(letters, null);
List letters=array.asList('a','b','c');
列表结果=foo(字母,空);
我假设,如果我们遵循您最初的示例,结果将始终是一个空列表。我只是在纸上做了这些,所以我没有在代码中测试它。顺便说一句,我希望这个想法足够清楚。在遍历列表时修改列表是不安全的。您无法“检索”这些值,它们在删除后就会消失。总体而言,你想要完成什么?您不需要递归来重复删除列表的第一个元素,这就是我们所做的,而且您似乎没有任何类型的结束情况。最终,这段代码将删除列表中的每个元素并返回。@Carlos最终它将尝试删除列表中已删除的元素。你是对的,我忽略了在这种情况下大小保持不变的事实。