gpt4 book ai didi

java - 查找递归 void 方法的基本情况

转载 作者:搜寻专家 更新时间:2023-11-01 03:53:00 25 4
gpt4 key购买 nike

我正在做作业。我想为递归建立一个基本案例,其中按升序排列给定数字(list2)。编写这段代码的目的是,当所有数字都按升序排列时,应停止调用名为 ascending(list2, list1) 的方法; list2 中的所有值都应发送到 list1。例如,list2 = 6,5,4,3,2,1 那么 list2 变为空,list1 应该是 1,2,3,4,5,6。我正在尝试将结果与前一个进行比较,如果匹配则停止。但我找不到阻止它的基本案例。另外,ascending() 和 fixedPoint() 都是 void 方法。有人有想法吗?大声笑花了我 3 天...

然后当我运行我的代码时

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

无限......

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
}

收到建议后的第二次尝试。

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

//temp1打印出赋值

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

temp2 = list1;
// store previous value

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

//在调用 ascending() temp1 之后变成空的 大声笑 所以无法在 if 语句中进行比较....有人可以更正吗?

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

在与 dasblinkenlight、Julien S、Nikolas、ZouZou 和 vels4j 进行头脑 Storm 之后,找到了解决方案。感谢您的思想贡献! :-)

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

}

最佳答案

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

然后您应该添加一个循环来检查 list1 的元素是否按升序排列,如下所示:

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

关于java - 查找递归 void 方法的基本情况,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19764774/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com