Results 1 to 3 of 3
  1. #1
    fam2315 is offline Member
    Join Date
    Feb 2011
    Posts
    78
    Rep Power
    0

    Default Implementing a stack as an array

    If I was going to write my own stack class, but use an array to do so, wouldn't I have to initialize the array to some size in order to do so? But since a stack is virtually unbounded in size, implementing it using an array seems like a bad choice, would you agree?

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    16,589
    Rep Power
    23

    Default

    Underneath a lot of collection classes there are arrays. What that means is you have to be able to resize the array ie create a larger one and copy old one,
    or have some logic that will span/jump from one array to another depending on the index value. For example the first array would be for indexes 0-99, the next for indexes 100 to 599 etc. Your logic would convert an index of 102 to 2 and use the second array.

  3. #3
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I'm not 100% sure what you're trying to accomplish, but is there a reason why you can't use an ArrayList? Since the size isn't set in stone you would be able to make your needed array as big as you need.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

Similar Threads

  1. trouble creating stack using array
    By shashankc in forum New To Java
    Replies: 5
    Last Post: 01-20-2011, 12:49 PM
  2. Implementing a Stack Using two Queues
    By rhm54 in forum New To Java
    Replies: 3
    Last Post: 12-01-2010, 10:28 AM
  3. Need help please implementing a stack!
    By sfe23 in forum New To Java
    Replies: 0
    Last Post: 02-24-2009, 03:19 PM
  4. Replies: 1
    Last Post: 03-25-2008, 05:06 AM
  5. how to convert a Java array to a java stack?
    By pompeez in forum New To Java
    Replies: 2
    Last Post: 08-13-2007, 02:41 PM

Posting Permissions

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