Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: ArrayList recursion return help

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ArrayList recursion return help

    Hi Forums! Im in need of some help with two problems im trying to attempt.

    So the first problem im doing is the pascal's triangle using arraylist<integer> but when it prints out, it gives me the wrong output.
     
    public ArrayList<Integer> getPascal (ArrayList<Integer> rows)
      {
        if (rows.size() < 0)
          return rows;
     
        ArrayList<Integer> lastRow = new ArrayList<Integer>();
     
        lastRow.add(1);
        System.out.println(lastRow);
     
        for (int i = 1; i <= rows.size(); i++)
        {
          ArrayList<Integer> currentRow = new ArrayList<Integer>();
          currentRow.add(lastRow.get(0));
     
          for (int j = 1; j < i; j++)
          {
            currentRow.add(lastRow.get(j - 1) + lastRow.get(j));
          }
     
          currentRow.add(lastRow.get(i));
          lastRow = currentRow;
          System.out.println(currentRow);
     
        }
        return lastRow;
     
     
      }

    From the error compiler there's something wrong with " if (rows.size() < 0) { return; }. Now i know thats obviously wrong but how could i resolve it into ArrayList<Integer> terms? Ive tried a couple methods but they didnt work as well. For my driver, i just declared a new variable to print the result and it gave me [1] [1] going down a column. If anyone does have time on their hands, how would using one object of arraylist look like?

    Thank you very much!


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ArrayList recursion return help

    the error compiler
    Please post the full text of the error message.

    when it prints out, it gives me the wrong output.
    Please post the output and add some comments showing what is wrong with the output
    and show what the output should be.

  3. #3
    Junior Member
    Join Date
    Oct 2011
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ArrayList recursion return help

    For the out put it gave me:

    [1]

    [1]

    Where as the output i want to achieve is

    [1]
    [1] [1]
    [1] [2] [1]

    and so on.

    For the error there is none in this case since i fixed the issue. So what do i need to do in order to achieve that output? Is there a discrepancy in the code?

    Thanks!

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: ArrayList recursion return help

    Is there a discrepancy in the code?
    Yes I would say there is if it doesnt' do what you want it to do.

    Try debugging the code by adding printlns to show the values of variables and the execution flow as the program executes. If you know how the program is supposed to execute, the print outs should show you where the problem is.

Similar Threads

  1. Return Object does not return the expected output
    By Nour Damer in forum What's Wrong With My Code?
    Replies: 1
    Last Post: October 13th, 2011, 07:24 AM
  2. [SOLVED] How to get the return of a method inside an object in an ArrayList?
    By Hallowed in forum Java Theory & Questions
    Replies: 7
    Last Post: May 1st, 2011, 10:44 PM
  3. Ordering ArrayList by 3 conditions as you add to ArrayList
    By aussiemcgr in forum Collections and Generics
    Replies: 4
    Last Post: July 13th, 2010, 02:08 PM
  4. ArrayList Unexpected Return
    By Cammack in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 31st, 2010, 09:23 PM
  5. How to use an ArrayList and what is its advantage over array?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 1
    Last Post: May 17th, 2009, 01:12 PM

Tags for this Thread