Results 1 to 6 of 6
  1. #1
    shashankc is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Exclamation trouble creating stack using array

    i was trying to create a stack using a array of objects of a class called "stock".
    public class stock{
    int price;
    int qty;
    }

    in public static void main(), i was creating an object of the class:
    stock a= new stock();
    and accepting values for a.price and a.qty from user.
    i would then push the object 'a' into stack.

    This was in a loop. i would ask the user to enter fresh values for a.price and a.qty and then perform a push operation, stack1.push(a).
    However, i noticed that when i printed the elements of the stack, they were all the same, which was the last value a.

    were all the elements of the stack somehow pointing to 'a'?

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    If you only did:
    stock a= new stock();
    once and then used this same object inside your loop then all you were doing with:
    a.price = whatever the user entered;
    was changing the price attribute of the same object each time.

    So yes, you were adding the same object to the stack, and changing it's attributes each time round the loop.

    Move the declaration of 'a' inside the loop.

  3. #3
    quad64bit's Avatar
    quad64bit is offline Moderator
    Join Date
    Jul 2009
    Location
    VA
    Posts
    1,323
    Rep Power
    7

    Default

    Tolls is probably correct, but repeatedly calling stack.peek() would also create the same behavior :D - make sure you are really popping them, or are using some form of iterator.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default

    Quote Originally Posted by quad64bit View Post
    Tolls is probably correct...
    "probably"?
    "PROBABLY"?!?

    The cheek...

    ;)

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

    Default

    Quote Originally Posted by Tolls View Post
    "probably"?
    "PROBABLY"?!?

    The cheek...

    ;)
    He misspelled "accidentally" ...

    kindest regards,

    Jos ;-)
    cenosillicaphobia: the fear for an empty beer glass

  6. #6
    shashankc is offline Member
    Join Date
    Jan 2011
    Posts
    2
    Rep Power
    0

    Default

    ha!ha! Tolls..u probably did get it right.....thx for solving the problem..

    cheers
    shashank

Similar Threads

  1. Trouble creating object that includes array
    By Desdenova in forum New To Java
    Replies: 7
    Last Post: 05-18-2010, 08:33 PM
  2. Trouble with creating TreeSet
    By MrKP in forum New To Java
    Replies: 1
    Last Post: 12-27-2009, 03:22 AM
  3. Replies: 2
    Last Post: 02-23-2009, 03:18 AM
  4. Replies: 1
    Last Post: 03-25-2008, 06:06 AM
  5. Creating a stack with data
    By ai_2007 in forum Advanced Java
    Replies: 2
    Last Post: 07-02-2007, 04:28 PM

Tags for this Thread

Posting Permissions

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