java - 在java中使用递归反转整数列表数组

标签 java recursion

如果有任何帮助,我们将不胜感激。我是一年级编程学生,这是一项研究递归的家庭作业。 1.我们必须递归地构建一个数组列表(在下面完成并解决最小的问题,然后是更高的问题) 2.使用线束进行测试(也在下面) 3. 然后我们必须克隆数组列表(我认为这是有效的)并编写一个方法来递归地反转数组列表并将其包含在 ListMethodRunner 测试中;这就是我被困住的地方。我在下面包含了我的代码,该代码在 'list = reverseList(list.remove(0));' 上出现错误在reverseList方法中。有什么建议我哪里出错了吗?

//列出方法 导入java.util.*;

public class ListMethods
{
//new method that produces an array list of integers (tempList) based on input of int n
public static ArrayList<Integer> makeList(int n)
{
  ArrayList<Integer> tempList = null;
  if (n <= 0)  // The smallest list we can make
  {

      tempList = new ArrayList<Integer>(); // ceate the list tempList
      return tempList;                      //return blank list for this if statement

  }
  else        // All other size lists are created here
  {

      tempList = makeList(n-1); //recursively go through each variation of n from top down, when reach 0 will create the list
      tempList.add(n); // program will then come back up through the variations adding each value as it goes

  }
  return tempList; //return the tempList population with all the variations

   }

//create a copy of the values in the array list (tlist) and put it in (list)- used in next method
public static ArrayList<Integer> deepClone(ArrayList<Integer> tList)
{
   ArrayList<Integer> list = new ArrayList<Integer>();
   for (Integer i : tList)
   {
       list.add(new Integer(i));
    }
    return list;
}
//method that creates arraylist
   public static ArrayList<Integer> reverseList(ArrayList<Integer> tList)
  {
   ArrayList<Integer> list = ListMethods.deepClone(tList);
 if (list.size()<=1)    // The list is empty or has one element
   {
      return list;// Return the list as is – no need to reverse!
 }
 else
 {
   list = reverseList(list.remove(0)); //recurse through each smaller list 
                                        //removing first value until get to 1 and will create list above
   list.add(0);
  // Use the solution to a smaller version of the problem
 // to solve the general problem
 }
 return list;
 }
}

//List  Methods Runner


import java.util.ArrayList;

public class ListMethodsRunner
{
 public static void main(String[] args)
{
  ArrayList<Integer> tempList = ListMethods.makeList(100);
  if (tempList.size() == 0)
  {
      System.out.println("The list is empty");
  }
  else
  {
     for (Integer i : tempList)
     {
        System.out.println(i);
     }
  }

     }
}

最佳答案

替换

list = reverseList(list.remove(0))

list.remove(0);
list = reverseList(list);

ArrayList::remove(int)返回从列表中删除的元素。 (在本例中输入 Integer)

reverseList需要 ArrayList<Integer>作为参数。因此错误。

您还必须在再次插入元素之前存储该元素。您的代码应如下所示:

Integer num = list.remove(0);
list = reverseList(list); 
list.add(num);

关于java - 在java中使用递归反转整数列表数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14921197/

相关文章:

java.lang.IllegalStateException : Couldn't create Engine

java - 如何处理 Adf bootstrap 元数据 adfc-mobile-config.xml 为 null 异常

java - 选取相关对象的前N个元素

c++ - 编写递归 C++ 函数,计算并返回数组中整数的乘积

java - Java 中用于解决填字游戏的递归回溯

Java 安卓错误 "too much data for RSA block"

java - Apache Tomcat、Eclipse、JBoss 和 RichFaces

java - 在 int 变量上使用时,Volatile 关键字的行为不符合预期

Python递归生成器性能

c - OpenMP 线程 ID 如何与递归一起使用?