Find a base case for a recursive void method

Posted by Evan S on Stack Overflow See other posts from Stack Overflow or by Evan S
Published on 2013-11-04T09:28:18Z Indexed on 2013/11/04 9:54 UTC
Read the original article Hit count: 247

Filed under:
|
|

I am doing homework. I would like to build a base case for a recursion where ordering given numbers (list2) in ascending order. Purpose of writing this codes is that when all numbers are in ascending order then should stop calling a method called ascending(list2, list1); and all values in list2 should be shipped to list1. For instance, list2 = 6,5,4,3,2,1 then list2 becomes empty and list1 should be 1,2,3,4,5,6. I am trying to compare result with previous one and if matches then stop. But I can't find the base case to stop it. In addition, Both ascending() and fixedPoint() are void method. Anybody has idea? lol Took me 3 days...

When I run my code then

6,5,4,3,2,1

5,6,4,3,2,1

4,5,6,3,2,1

3,4,5,6,2,1

2,3,4,5,6,1

1,2,3,4,5,6

1,2,3,4,5,6

1,2,3,4,5,6

1,2,3,4,5,6

1,2,3,4,5,6

infinite.............

public class Flipper
{

public static void main(String[] args)
{   
    Flipper aFlipper = new Flipper();

    List<Integer> content = Arrays.asList(6,5,4,3,2,1);
    ArrayList<Integer> l1 =  new ArrayList<Integer>(content);
    ArrayList<Integer> l2 = new ArrayList<Integer>(); // empty list

    aFlipper.fixedPoint(l2,l1);

    System.out.println("fix   l1 is "+l1);
    System.out.println("fix   l2 is "+l2);
}


public void fixedPoint(ArrayList<Integer> list1, ArrayList<Integer> list2)
{
    // data is in list2
    ArrayList<Integer> temp1 = new ArrayList<Integer>(); // empty list

    if (temp1.equals(list2)) 
    {
        System.out.println("found!!!");             
    }

    else
    {
        ascending(list2, list1); // data, null
        temp1 = list1; // store processed value
        System.out.println("st list1 is "+list1);
        System.out.println("st list2 is "+list2);
    }
    fixedPoint(list2, list1); // null, processed data       
}

© Stack Overflow or respective owner

Related posts about java

Related posts about methods