4

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       
}

Second try after receiving advice.

else        {
            temp1 = list2;
            System.out.println("temp1: "+temp1); 

// temp1 printed out the value assigned

            // store only previous value
            ascending(list2, list1); // data, null

            temp2 = list1;
            // store previous value

            System.out.println("temp1: "+temp1); 

// after invoking ascending() temp1 becomes empty lol So not able to compare in if statement.... Can anybody correct it?

            System.out.println("temp2: "+temp2);
        }
        fixedPoint(list2, list1); // previous, proceeded data

After brain storming with dasblinkenlight, Julien S, Nikolas, ZouZou and vels4j a solution found. I appreciate your contribution of thought! :-)

public void fixedPoint(ArrayList<Integer> list1, 
                       ArrayList<Integer> list2)
    {
        List<Integer> content = Arrays.asList(1);
        ArrayList<Integer> temp1 = new ArrayList<Integer>(content); 
        fixedPoint(list2, list1, temp1);
    }
    // Since it is recursive method I needed to create another parameter
    // to store temporary values.
    public void fixedPoint(ArrayList<Integer> list1, 
                           ArrayList<Integer> list2, 
                           ArrayList<Integer> temp)
    {


        ArrayList<Integer> temp1 = new ArrayList<Integer>();
        temp1 = temp;

        if (temp1.equals(list2))
        {
            return;
        }

        else
        {
            temp1.clear(); 
            for(int i = 0; i < list2.size(); i++) 
            // To store temp value of list2, 
            // I used add method.  Because ArrayList is an object type so if I assign 
            // list2 to temp1 then it will assign memory address rather 
            // than values.  Thus I will lose the values after invoking ascending() as 
            // all elements of list2 will shipped to list1.  So List2 becomes empty.
            {
                temp1.add(list2.get(i));
            }
            ascending(list2, list1);

            fixedPoint(list2, list1, temp1);
        }

    }
Evan S
  • 191
  • 1
  • 5
  • 12

3 Answers3

0

Your problem probably arises from the usage of temp1.equals(list2). What you want to use is Arrays.equals(temp1, list2).

An explanation for this is given here by Peter Lawrey.

Edit Yeah, I should probably read better.

I just checked and it appears ArrayList inherits .equals() from List which is defined differently than array.equals() and "should" work.

Community
  • 1
  • 1
Nikolas Grottendieck
  • 2,548
  • 1
  • 24
  • 24
  • Arrays.equals won't work because temp1 and list2 are ArrayLists and no Array. CollectionsUtils.isEqualCollection works perfectly in this case. – Julien Nov 04 '13 at 09:40
  • @Nikolas Thanks for your suggestion. I noted for use of Arrays next time :-) – Evan S Nov 04 '13 at 09:52
0

There is no return in fixedPoint when the case is found. Therefore the line fixedPoint(list2, list1); will be processed regardless of the fixed point. I am not able to test since ascending method is not provided, however I think that

if (CollectionUtils.isEqualsCollection(list1,list2) 
{
    System.out.println("found!!!"); 
    return;            
}

would do the job.

You will require Apache-commons Collections to perform the equality on lists with isEqualsCollection.

Julien
  • 1,302
  • 10
  • 23
  • return may causes the issue. However what is wrong with `temp1.equals(list2)` ? – vels4j Nov 04 '13 at 09:43
  • You can use equals to compare ArrayList, it has been overridden in `AbstractList.java.` – Alexis C. Nov 04 '13 at 09:45
  • @Julien S. I am getting this messeage...if (CollectionUtils.isEqualsCollection(list1,list2)) ^ symbol: variable CollectionUtils location: class Flipper 1 error – Evan S Nov 04 '13 at 09:49
  • @ZouZou I tested equals method with ArrayList yup it worked. So I applied but do you see any logic issue I should correct? – Evan S Nov 04 '13 at 09:51
  • @EvanS, did you import the apache commons collection library into your project ? – Julien Nov 04 '13 at 12:43
  • @Julien S I have no idea apache commone collection lib...Could you give me a full statement start with import.? – Evan S Nov 07 '13 at 00:21
  • Import the library from here http://commons.apache.org/proper/commons-collections/ in your project. Your IDE will complete the import. – Julien Nov 07 '13 at 10:58
0

Purpose of writing this codes is that when all numbers are in ascending order then should stop calling a method called ascending(list2, list1)

Then you should add a loop that checks for the elements of list1 to be in ascending order, like this:

public void fixedPoint(ArrayList<Integer> list1, ArrayList<Integer> list2)
{
    boolean isAscending = true;
    for (int i = 1 ; (isAscending) && (i < list2.size()) ; i++) {
        isAscending = list2.get(i-1) < list2.get(i);
    }
    if (isAscending) {
        ... // Insert code to copy the data from list2 to list1.
        ... // Note that a simple assignment is not going to work here!
        System.out.println("found!!!");
        return;
    }
    // It's not in ascending order - continue recursing down.
    ascending(list2, list1);
    ArrayList<Integer> temp1 = new ArrayList<Integer>(list1); // store processed value
    fixedPoint(list2, list1);
    // temp1 makes the old value of list1 available for comparison
    System.out.println("st list1 is "+list1);
    System.out.println("st list1 was "+temp1);
    System.out.println("st list2 is "+list2);
}
Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
  • Thanks for your idea. I noted your codes. Buy can you think of way to compare previous result and list of numbers will passed in? – Evan S Nov 04 '13 at 09:55
  • @EvanS Recursive code has no "previous" state, each level of invocation must deal only with its own data passed as parameters. You can definitely compare data before and after calling `fixedPoint` recursively, though - for that you need to make a copy of the list into a `temp` before the call, make the call, and then do the comparison. That has little to do with establishing your base case, though. – Sergey Kalinichenko Nov 04 '13 at 10:01
  • Thanks for your reply. Dude that's exactly what I am attempting here. Could you bring your idea into codes? Cos I am new to recursion. If I can see best example would be no better than any other explanation in this case. Thanks. – Evan S Nov 04 '13 at 10:06
  • @ dasblinkenlight Thanks for your update! It was applied for the codes but ended up with a linefeed and seemed stuck on that stage forever. But I learned from your codes ((isAscending) && (i < list2.size()) can be used as step statement for a for-loop. Cool. I figured out a solution will be pasted soon :-) Thanks for your participation. – Evan S Nov 07 '13 at 00:19