Homework Help: In Java, should I use a stack when I would normally use an ArrayList?

Discussion in 'Mayberry Lounge' started by MATT DAMON, Aug 29, 2015.

  1. MATT DAMON

    MATT DAMON Blue Belt

    Joined:
    Jun 10, 2015
    Messages:
    552
    Likes Received:
    235
    I'm learning a lot of the Stack data structure, and most importantly, I'm seeing that it can be used to store elements just as any other storage data structure is used.


    Now, should I use it instead of an ArrayList, or an Array when the data items are small in nature?

    I think this is unconventional, but it is worth a try.
     
  2. Liquid Smoke

    Liquid Smoke Great artists steal™

    Joined:
    Jul 11, 2013
    Messages:
    15,237
    Likes Received:
    18,958
    Technically a stack can only access data in a first in/last out fashion, like a stack of dishes. That's a pretty serious limitation if you need to get an item from the middle. If you require that type of functionality (like on an undo list) then that is the correct choice.

    Not sure about Java's implementation of an ArrayList, but I'm guessing you have more options for accessing its items.
     
  3. Was Exiled

    Was Exiled Brown Belt

    Joined:
    May 30, 2014
    Messages:
    3,037
    Likes Received:
    821
    Location:
    Ireland
    Seeing as Stack is LIFO there's only going to be very specific situations where that's ideal (i.e. when implementing a stack). With Arraylist's you can remove and add at specific indexes and it does the resizing etc. for you.
     
  4. brothir

    brothir Purple Belt

    Joined:
    Oct 13, 2014
    Messages:
    1,650
    Likes Received:
    1,253
    Location:
    Norway
    I checked the memory usage and time usage of the Stack and ArrayList classes, and got the following numbers on my PC:

    Stack: 83 886 000
    ArrayList: 70 091 000

    The above numbers are the amount of Integer objects that could be added before I got an OoM error.

    These are the times I got for the push() and add() methods, respectively.

    Median stack time: 583
    Median array list time: 582

    These numbers were gotten using System.nanoTime(), and taking the median of 101 tries.

    As you can see, Stack is a bit more memory efficient in being able to store almost 14 million Integer objects more, while being equally quick in adding new elements.

    Overall, I can't say I would recommend using Stack unless it is a natural fit for the task. The trade in flexibility for memory efficiency is not good enough imo.
     
  5. Michaelangelo

    Michaelangelo Okay USA Platinum Member

    Joined:
    Sep 8, 2010
    Messages:
    26,099
    Likes Received:
    46,986
    What is the problem?
     
  6. MATT DAMON

    MATT DAMON Blue Belt

    Joined:
    Jun 10, 2015
    Messages:
    552
    Likes Received:
    235
    Not really a problem, but more of a general discussion about using Stacks, and ArrayLists.

    I'm really liking Stacks, but I tend to forget that each data structure has certain limitations that we must keep in mind.


    Thanks for posting.
     
  7. Michaelangelo

    Michaelangelo Okay USA Platinum Member

    Joined:
    Sep 8, 2010
    Messages:
    26,099
    Likes Received:
    46,986
    Stacks are like a Pez dispenser
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.