atom2ueki
atom2ueki

Reputation: 837

Java: Recursive never hit a correct condition

Here is my code for a self-recursive method.problem is the 3rd recursive,compare <3.0, 4.0, 4.0> and <2.0, 4.0, 1.0>,it should hit case 3, but from the logs shown, it never hit it.

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;
    }

the catlog output below

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

The third recursive should hit case 3, but from the log result, it seems never hit any condition, very strange.

Upvotes: 1

Views: 104

Answers (3)

atom2ueki
atom2ueki

Reputation: 837

After i change for (int i=count+overlapnum+1 ;i<sortedlist.size()-count-overlapnum;i++) to for (int i=count+overlapnum+1 ;i<sortedlist.size()-count;i++) it has hit case3, but also hit case1 after hit case3, now i=3;i<4;i++, it should only hit case3 and end this recursion.

the new cat logs here

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

Upvotes: 0

Nikhil
Nikhil

Reputation: 2308

Quick look through, it seems the third recursive call will never enter the for loop, because i = (2 + 0 + 1) and the loop condition check i < (4 - 2 - 0) will fail.

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

Upvotes: 0

Tyler Lee
Tyler Lee

Reputation: 2785

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<2 is false. Your for never runs, hence no condition getting hit.

Upvotes: 2

Related Questions