Java:递归永远不会达到正确的条件

时间:2013-09-30 04:39:03

标签: java recursion arraylist

这是我的自递归方法的代码.problem是第3个递归,比较<3.0, 4.0, 4.0><2.0, 4.0, 1.0>,它应该点击案例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;
    }

下面的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>]

第三个递归应该是hit case 3,但从日志结果来看,似乎从来没有遇到任何条件,非常奇怪。

3 个答案:

答案 0 :(得分:2)

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++)

3&lt; 2是假的。你从不运行,因此没有任何条件被击中。

答案 1 :(得分:0)

快速查看,似乎第三次递归调用永远不会进入for循环,因为i =(2 + 0 + 1)并且循环条件检查i&lt; (4 - 2 - 0)将失败。

for (int i=count+overlapnum+1 ;i<sortedlist.size()-count-overlapnum;i++)
    //   i = 3                ; i < 2
    // for loop is never entered

答案 2 :(得分:0)

在我将(int i=count+overlapnum+1 ;i<sortedlist.size()-count-overlapnum;i++)更改为for (int i=count+overlapnum+1 ;i<sortedlist.size()-count;i++)之后,它已经命中了case3,但在命中case3之后也命中了case1,现在i = 3; i <4; i ++,它应该只命中case3并结束这个递归。

新猫在这里登录

**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>]