Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Help with recursive reversal of arraylist..

Name: not autistic enough 2011-05-06 17:25

why does this not work

   public static ArrayList<Integer> reverseList(ArrayList<Integer> tList)
{
   ArrayList<Integer> list = ListMethods.deepClone(tList);
   if (list.size() <= 1)
   {
      return list;

   }
   else
   {
      list.add(list.size(), list.get(0));
      list.remove(0);
      reverseList(list);
   }
   return list;
}

Exception in thread "main" java.lang.StackOverflowError
        at java.util.ArrayList.get(ArrayList.java:322)
        at java.util.AbstractList$Itr.next(AbstractList.java:345)
        at ListMethods.deepClone(ListMethods.java:29)
        at ListMethods.reverseList(ListMethods.java:38)
        at ListMethods.reverseList(ListMethods.java:48)

Name: Anonymous 2011-05-06 17:30

What's a StackOverflowError? I never got that in Scheme.

Name: Anonymous 2011-05-06 17:33

Recursion

Hahaha

Name: Anonymous 2011-05-06 17:37

The list will never have a size smaller than itself.
list.add(list.size(), list.get(0)); (+1)
list.remove(0); (-1)
Unless deepCopy does something profound, this function will just clutter the stack with frames containing copies of the initial list until it overflows.

Name: Anonymous 2011-05-06 17:38

>>4
DON'T HELP HIM!

Name: Anonymous 2011-05-06 17:39

>>2
It's the error you get when you should have posted this shit at StackOverflow.

Name: Anonymous 2011-05-06 17:41

>>2
funny guy there.  Believe me I'm tired of java at this point,  I appreciate that it can make me money, has a nice library, and dev tools and that's about it.

Basically what's happening is the array is reversing but it just keeps reversing over and over because there is no way for me to shrink the list to satisfy the base case. Our piece of shit professor assigns 20 pages of java problems every week without even looking at what he's doing.   He really sucks the fun out of this shit and makes me not care.  There is a simple "do this instead" solution for this but since I have 19 more pages of this crap I never have time to sit down and think and enjoy the process.  (our professor also "secretly" doesn't know java)

While we're learning discrete math and JVM assembly from other more reasonable professors.  Literally 90% of my time goes to java and 8% discrete math 2% assembly.

Name: Anonymous 2011-05-06 17:47

>>5
Yeah because there is a ton of other great programming discussion here, you're the reason sussman thinks /prog/ is full of fags... also:
ENTERPRISE PROGRAMMER
CUDDER CUDDER CUDDER CAR CAR EVAL APPLY!!!!!
also frozenvoid buO00O0O02@faggot synACKsynACKsynACK ylpeR

Name: Anonymous 2011-05-06 17:50

>>8
You just upset an experienced coder.

Name: Anonymous 2011-05-06 17:51

>>6
It's because it never stops recursing and keeps loading up the stack.  This btw is the 2nd recursion problem from the book.. which is fucking horrid since it involves issues not directly specific to recursion.

too much extra shit to keep in my head at once when I'm trying to learn something.

Name: Anonymous 2011-05-06 17:52

>>8
You're an idiot if you don't realize the SUSSMAN is Frozenvoid and love us dearly!

Name: Anonymous 2011-05-06 17:58

>>8
Back to /lounge/, please!

Name: Anonymous 2011-05-06 18:02

>>12
Thread.bump()

>>11
It all makes sense now.

Name: Anonymous 2011-05-06 18:13

// を確認してくださいぶつけているパンツ
bampu(&pantsu, NULL, NULL, NULL, NULL, NULL, NULL);

Name: Anonymous 2011-05-06 18:22

Originally pass the list length or something, then just decrement that every pass.

Name: Anonymous 2011-05-06 19:31

That's weird. Do you really have to do it this way with deepclone (maybe those were 'hints')? Here's pseudo-code to recursively reverse a list:

reverse list =
    if(list is empty) return list
    else return concatenate(reverse tail(list), head(list))

Name: Anonymous 2011-05-06 22:47

Pseudocode:
reverse list = fold_left prepend empty_list list

The implementation of fold_left is left as an exercise for the reader.

Name: Anonymous 2011-05-06 23:18

OP here I figured it out.. no I really did, thanks though.

Name: Anonymous 2011-05-07 1:35

Why are you trying to sort a list using recursion?

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List