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