11.04. ArrayList and Stack Overview

ArrayList and stack overview

  • Eclipse: Oxygen
  • Java: 1.8

ArrayList

As we know the size of an array is fixed. So if we wish to have a variable-length data structure so that we can store the elements in an array dynamically we make use of ArrayList. In this tutorial, we will discuss ArrayList. 

ArrayList is a part of the collection framework present in java.util package. In java, it provides dynamic arrays. An ArrayList is an object which can be a resizable array. It is an implementation of List interface where the elements can be dynamically added or removed from the List. Also, the size of the list is increased dynamically if the elements are added more than the initial size. 

The ArrayList shown earlier has some disadvantages such as very slow deletion or addition.

ArrayList is a very important data structure useful in handling the dynamic behavior of elements. However, it can be slower than standard arrays, but it can be useful in programs where a lot of manipulation is needed.

Pros

  • Fast
  • Easy to access any element from any location

Cons

  • Operations are slow
  • Tends to use more memory while operating

Stack

We see the stack data structure here. The properties of a stack data structure are shown here and there some of the operations that include the search, insertion, deletion, and the front element in the stack are shown here.

Java Stack is a LIFO (Last in first out) object. Extends the Vector class but supports only five operations. The Java stack class only has one constructor that is empty or the default constructor. Then, when we create a Stack, initially it does not contain elements that mean that the Stack is empty.

Let’s understand the method by the following table.

   Modifier and Type                                   Method and Description
boolean Empty( ) Tests if this stack is empty.
E Peek( )Looks at the object at the top of this stack without removing it from the stack
E Pop( ) Removes the object at the top of this stack and returns that object as the value of this function.
E Push( E item) Pushes an item onto the top of this stack.
int search(object o) Returns 1 – based position where an object is on this stack.

Contributed by: Poonam Tomar

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments