Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在java中使用递归反转整数listarray_Java_Recursion - Fatal编程技术网

在java中使用递归反转整数listarray

在java中使用递归反转整数listarray,java,recursion,Java,Recursion,非常感谢您的帮助。我是编程一年级的学生,这是一个关于递归的家庭作业。 1.我们必须递归地构建一个arraylist(在下面完成,针对最小的问题,然后针对更高的问题) 2.使用线束(也在下面)对其进行测试 3.然后,我们必须制作数组列表的克隆副本(我认为这是可行的),并编写一个递归反转arraylist的方法,并将其包含在ListMethodRunner测试中;这就是我被困的地方。我已经在下面包含了我的代码,它在“list=reverseList(list.remove(0));”上引发了一个错误

非常感谢您的帮助。我是编程一年级的学生,这是一个关于递归的家庭作业。 1.我们必须递归地构建一个arraylist(在下面完成,针对最小的问题,然后针对更高的问题) 2.使用线束(也在下面)对其进行测试 3.然后,我们必须制作数组列表的克隆副本(我认为这是可行的),并编写一个递归反转arraylist的方法,并将其包含在ListMethodRunner测试中;这就是我被困的地方。我已经在下面包含了我的代码,它在“list=reverseList(list.remove(0));”上引发了一个错误在反向列表法中。有什么建议我哪里出了问题吗

//列表方法 导入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);
     }
  }

     }
}
公共类ListMethods
{
//基于int n的输入生成整数数组列表(tempList)的新方法
公共静态ArrayList生成列表(int n)
{
ArrayList tempList=null;
如果(n更换

list = reverseList(list.remove(0))

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

reverseList
需要一个
ArrayList
作为参数。遍历错误

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

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

list = reverseList(list.remove(0))

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

reverseList
需要一个
ArrayList
作为参数。遍历错误

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

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

list=reverseList(list.remove(0));
=>什么错误?是编译错误吗?运行时异常?消息说什么?很抱歉,它想将reverseList更改为int,这在
list=reverseList(list.remove(0))上导致错误什么错误?是编译错误吗?运行时出现异常?消息怎么说?很抱歉,它想将reverseList更改为intCode all working and have run test,但在reverse函数中获得初始值100后,它们都是0。在递归过程中似乎没有添加值。我应该从最后一个开始吗值和向后工作?@user1836661编辑的答案。请检查。编码所有工作并已运行测试,但在反向函数中获得初始值100后,它们都是0。在递归过程中,似乎没有添加值。我是否应该从最后一个值开始向后工作?@user1836661编辑的答案。请检查。