Results 1 to 7 of 7
  1. #1
    boss is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default Arraylist of stacks

    I'm trying to solve the "Hanoi Moves" challenge.
    I'm using an Arraylist of stacks to implement it, but for some reason it's not building the stacks as it should. Instead, it's placing all the discs in all the stacks, so they all end up [6,5,4,3,2,1].
    Here's the portion of the code that builds the stacks (or should):

    for (int i=inicio.size()-1;i>=0;i--) { // i = disc radius, <inicio> contains the initial pegs to which discs belong.
    peg = (Integer)inicio.get(i); // peg = stack to which the disc belongs
    (stacks.get(peg)).push(i+1); // it pushes all the disks onto all the stacks, instead of only <peg>
    System.out.println(peg);
    System.out.println(stacks.get(peg));


    What's wrong?
    Last edited by boss; 10-07-2013 at 06:48 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arraylist of stacks

    I don't understand your code: according to that code snippet 'peg' is an Integer or an int but in your comment you say 'peg' is supposed to be a stack ... show us a bit more code please.
    (b.t.w. I think that you only have one stack stored multiple times in your ArrayList).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    boss is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default Re: Arraylist of stacks

    No, peg represents the stack to be pushed with the disc i. The stacks are stored in<stacks>.
    Here goes the code...

    ArrayList<Stack> stacks = new ArrayList<Stack>();
    ArrayList inicio = new ArrayList(8);

    int peg;
    Stack s0 = new Stack();
    for (int i=0;i<=K;i++) stacks.add(s0); // creates K empty stacks
    System.out.println(stacks); // OK

    for (int i=inicio.size()-1;i>=0;i--) { // i = disc radius, <inicio> contains the initial pegs to which discs belong
    peg = (Integer)inicio.get(i); // peg ids stack to push with disc i
    (stacks.get(peg)).push(i+1);
    System.out.println(peg); // OK

    System.out.println(stacks.get(peg));
    }

    // K and <inicio> are inputs.
    Last edited by boss; 10-07-2013 at 07:36 PM.

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,436
    Blog Entries
    7
    Rep Power
    20

    Default Re: Arraylist of stacks

    As I wrote: you're adding the single stack s0 to the ArrayList multiple times. Change that line to:

    Java Code:
    for (int i=0;i<=K;i++) stacks.add(new Stack());
    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    boss is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default Re: Arraylist of stacks

    Input for 6 discs and 4 pegs:
    6 4
    2 3 1 4 2 3 // stored in <inicio>


    Here's the output I get...

    [[], [], [], [], []] // empty stacks
    3 // last peg in <inicio>
    [6]
    2 // peg
    [6, 5]
    4 // peg
    [6, 5, 4]
    1 // peg
    [6, 5, 4, 3]
    3 // peg
    [6, 5, 4, 3, 2]
    2 // 1st peg in <inicio>
    [6, 5, 4, 3, 2, 1]
    [[6, 5, 4, 3, 2, 1], [6, 5, 4, 3, 2, 1], [6, 5, 4, 3, 2, 1], [6, 5, 4, 3, 2, 1], [6, 5, 4, 3, 2, 1]] // all the discs go in all the stacks

    it should end like this:
    stack 1: disc [3]
    stack 2: discs [5 1]
    stack 3: discs [6 2]
    stack 4: disc [4]
    Last edited by boss; 10-07-2013 at 07:50 PM.

  6. #6
    boss is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

    Default Re: Arraylist of stacks

    And that affects the push's lateron?

  7. #7
    boss is offline Member
    Join Date
    Jan 2010
    Posts
    12
    Rep Power
    0

Similar Threads

  1. Can somebody help me with stacks?
    By sim18 in forum New To Java
    Replies: 3
    Last Post: 10-08-2012, 07:06 PM
  2. Replies: 12
    Last Post: 07-07-2011, 08:49 PM
  3. Help with stacks
    By kMel90 in forum New To Java
    Replies: 3
    Last Post: 12-05-2010, 01:02 AM
  4. Help with stacks
    By Srcee in forum New To Java
    Replies: 5
    Last Post: 11-01-2009, 11:23 AM
  5. Using Stacks
    By ravian in forum New To Java
    Replies: 7
    Last Post: 11-28-2007, 09:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •