0

Would appreciate any help with this. Am first year programming student and this is a homework assignment to look at recursion. 1. we had to build an arraylist recursively (done below and working for smallest problem and then higher) 2. test it using a harness (also below) 3. we then have to make a clone copy of the array list (which I think is working) and write a method to recursively reverse the arraylist and include it in the ListMethodRunner test; this is where I am stuck. I have included my code below which is brining up an error on 'list = reverseList(list.remove(0));' in the reverseList Method. Any suggestions where Im going wrong?

//List Methods import 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);
     }
  }

     }
}
2
  • 1
    which is brining up an error on list = reverseList(list.remove(0)); => What error? Is it a compile error? An exception at runtime? What does the message say? Commented Feb 17, 2013 at 12:41
  • Sorry it wanted to change reverseList to an int Commented Feb 17, 2013 at 13:03

1 Answer 1

3

Replace

list = reverseList(list.remove(0))

With

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

ArrayList::remove(int) returns the element that was removed from the list. (type Integer in this case)

reverseList needs an ArrayList<Integer> as parameter. Ergo the error.

You also have to store the element before inserting it again. Your code should look like this:

Integer num = list.remove(0);
list = reverseList(list); 
list.add(num);
Sign up to request clarification or add additional context in comments.

2 Comments

Code all working and have run test, but after get initial value of 100 in the reverse function they are all 0's. values don't seem to be adding in during the recursion. Should I maybe start with the last value and work backwards?
@user1836661 Edited answer. Please check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.