1. The official Sherdog Store is back! Check it out! » Discuss it here! »

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 Blue Belt

    Joined:
    Jun 10, 2015
    Messages:
    552
    Likes Received:
    0
    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 Great artists steal™

    Joined:
    Jul 11, 2013
    Messages:
    15,203
    Likes Received:
    1
    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 Brown Belt

    Joined:
    May 30, 2014
    Messages:
    3,036
    Likes Received:
    0
    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 Purple Belt

    Joined:
    Oct 13, 2014
    Messages:
    1,653
    Likes Received:
    3
    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 Okay USA Platinum Member

    Joined:
    Sep 8, 2010
    Messages:
    26,580
    Likes Received:
    444
    What is the problem?
     
  6. MATT DAMON Blue Belt

    Joined:
    Jun 10, 2015
    Messages:
    552
    Likes Received:
    0
    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 Okay USA Platinum Member

    Joined:
    Sep 8, 2010
    Messages:
    26,580
    Likes Received:
    444
    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.