Java:递归从未达到正确的条件
这是我的自递归方法的代码。问题是第三个递归,比较Java:递归从未达到正确的条件,java,recursion,arraylist,Java,Recursion,Arraylist,这是我的自递归方法的代码。问题是第三个递归,比较和,它应该出现在案例3中,但从显示的日志来看,它从未出现过 private static List<Position> overlaps (List<Position> sortedlist) { List<Position> tony = new ArrayList<Position>(); if(count<=sortedlist.size()
和
,它应该出现在案例3中,但从显示的日志来看,它从未出现过
private static List<Position> overlaps (List<Position> sortedlist)
{
List<Position> tony = new ArrayList<Position>();
if(count<=sortedlist.size()-1)
{
//handle a
Position a = sortedlist.get(count);
System.out.println("**new recursive start!");
System.out.println("sortedlist size is:"+sortedlist.size());
System.out.println("overlapnum is:"+overlapnum);
System.out.println("count number is:"+count);
System.out.println("the sortedlist of this term:"+sortedlist);
//check from top to bottom
for (int i=count+overlapnum+1 ;i<sortedlist.size()-count-overlapnum;i++)
{
//case1
//and
// ------
// ------
if ( a.start()>sortedlist.get(i).start() &&a.start()<sortedlist.get(i).end() && a.end()>sortedlist.get(i).end() && a.height()>sortedlist.get(i).height() && a.equals(sortedlist.get(i))==false)
{
System.out.println("hit case1");
tony.add(new Position(a.start(), sortedlist.get(i).end(), a.height()-sortedlist.get(i).height()));
a= new Position(sortedlist.get(i).end(),a.end(),a.height());
sortedlist.set(count, a);
overlapnum++;
overlaps(sortedlist);
}
//case2
//and
//------
// ------
else if(a.end()>sortedlist.get(i).start() && a.end()<sortedlist.get(i).end() &&a.start()<sortedlist.get(i).start() && a.height()>sortedlist.get(i).height() && a.equals(sortedlist.get(i))==false)
{
System.out.println("hit case2");
//System.out.println(count);
tony.add(new Position(sortedlist.get(i).start(), a.end(), a.height()-sortedlist.get(i).height()));
a=new Position(a.start(),sortedlist.get(i).start(),a.height());
sortedlist.set(count, a);
overlapnum++;
overlaps(sortedlist);
}
//case3
// -------
//-----------
//***!!!!problem: why the third time recursive never hit case3?????
else if(a.start()>=sortedlist.get(i).start() && a.end()<=sortedlist.get(i).end() && a.height()>sortedlist.get(i).height() && a.equals(sortedlist.get(i))==false)
{
System.out.println("hit case3");
tony.add(new Position(a.start(),a.end(),a.height()-sortedlist.get(i).height()));
overlapnum=0;
count++;
}
//no overlaps found, directly write propping height
else
{
System.out.println("hit else");
tony.add(new Position(a.start(),a.end(),a.height()));
overlapnum=0;
count++;
}
}
return tony;
}
return null;
}
私有静态列表重叠(列表分类列表)
{
List tony=newarraylist();
if(countsortedlist.get(i).height()&&a.equals(sortedlist.get(i))==false)
{
System.out.println(“命中案例1”);
添加(新位置(a.start(),sortedlist.get(i).end(),a.height()-sortedlist.get(i).height());
a=新位置(sortedlist.get(i.end()、a.end()、a.height());
分类列表设置(计数,a);
重叠数++;
重叠(分类列表);
}
//案例2
//及
//------
// ------
否则如果(a.end()>sortedlist.get(i).start()&&a.end()=sortedlist.get(i).start()&&a.end()sortedlist.get(i).height()&&a.equals(sortedlist.get(i))==false)
{
System.out.println(“命中案例3”);
添加(新位置(a.start()、a.end()、a.height()-sortedlist.get(i.height()));
重叠数=0;
计数++;
}
//未发现重叠,直接写入支撑高度
其他的
{
System.out.println(“命中其他项”);
添加(新位置(a.开始(),a.结束(),a.高度());
重叠数=0;
计数++;
}
}
返回托尼;
}
返回null;
}
下面的catlog输出
**new recursive start!(1)
sortedlist size is:4
overlapnum is:0
count number is:0
the sortedlist of this turn:[<2.0, 5.0, 4.0>, <4.0, 7.0, 3.0>, <1.0, 3.0, 2.0>, <2.0, 4.0, 1.0>]
hit case2
**new recursive start!(2)
sortedlist size is:4
overlapnum is:1
count number is:0
the sortedlist of this turn:[<2.0, 4.0, 4.0>, <4.0, 7.0, 3.0>, <1.0, 3.0, 2.0>, <2.0, 4.0, 1.0>]
hit case1
**new recursive start!(3)
sortedlist size is:4
overlapnum is:2
count number is:0
the sortedlist of this turn:[<3.0, 4.0, 4.0>, <4.0, 7.0, 3.0>, <1.0, 3.0, 2.0>, <2.0, 4.0, 1.0>]
**新的递归开始!(1)
分类列表大小为:4
重叠数为:0
计数编号为:0
此回合的分类列表:[,,]
命中案例2
**新的递归开始!(2)
分类列表大小为:4
重叠数为:1
计数编号为:0
此回合的分类列表:[,,]
命中案例1
**新的递归开始!(3)
分类列表大小为:4
这个数字是:2
计数编号为:0
此回合的分类列表:[,,]
第三个递归应该
命中案例3
,但是从日志结果来看,它似乎从未命中任何条件,非常奇怪。对于(int i=count+overlapnum+1;i快速浏览,第三个递归调用似乎永远不会进入for循环,因为i=(2+0+1)并且循环条件检查i<(4-2-0)将失败
for (int i=count+overlapnum+1 ;i<sortedlist.size()-count-overlapnum;i++)
for (int i=0+2+1 ;i<4-0-2;i++)
for (int i=3 ;i<2;i++)
for (int i=count+overlapnum+1 ;i<sortedlist.size()-count-overlapnum;i++)
// i = 3 ; i < 2
// for loop is never entered
for(int i=count+overlapnum+1;i在我更改为(int i=count+overlapnum+1;i@nekizalb在我改变for循环方法之后,它工作了,但是在结果上仍然存在一些问题
**new recursive start!(1)
sortedlist size is:4
overlapnum is:0
count number is:0
the sortedlist of this term:[<2.0, 5.0, 4.0>, <4.0, 7.0, 3.0>, <1.0, 3.0, 2.0>, <2.0, 4.0, 1.0>]
hit case2
**new recursive start!(2)
sortedlist size is:4
overlapnum is:1
count number is:0
the sortedlist of this term:[<2.0, 4.0, 4.0>, <4.0, 7.0, 3.0>, <1.0, 3.0, 2.0>, <2.0, 4.0, 1.0>]
hit case1
**new recursive start!(3)
sortedlist size is:4
overlapnum is:2
count number is:0
the sortedlist of this term:[<3.0, 4.0, 4.0>, <4.0, 7.0, 3.0>, <1.0, 3.0, 2.0>, <2.0, 4.0, 1.0>]
hit case3
hit case1
**new recursive start!(4)
sortedlist size is:4
overlapnum is:1
count number is:1
the sortedlist of this term:[<3.0, 4.0, 4.0>, <3.0, 4.0, 4.0>, <1.0, 3.0, 2.0>, <2.0, 4.0, 1.0>]