Skip to content
Related Articles

Related Articles

Improve Article
Static and Dynamic data structures in Java with Examples
  • Last Updated : 06 Jul, 2020

Data structure is a way of storing and organising data efficiently such that the required operations on them can be performed efficiently with respect to time as well as memory. Simply, Data Structure is used to reduce complexity (mostly the time complexity) of the code.

Data structures can be two types:

Static Data structure

In the Static data structure, the size of the structure is fixed. The content of the data structure can be modified but without changing the memory space allocated to it.

Examples of Static Data Structures:

  1. Array

    An array is a container object that holds a fixed number of values of a single type. The length of an array is established when the array is created. An array is a group of like-typed variables that are referred to by a common name. Arrays in Java work differently than they do in C/C++.


    // Declaration
    type var-name[];
    type[] var-name;
    // Initialization
    var-name = new type [size];


    // Java program to illustrate creating an array
    // of integers, puts some values in the array,
    // and prints each value to standard output.
    class GFG {
        public static void main(String[] args)
            // declares an Array of integers.
            int[] arr;
            // allocating memory for 5 integers.
            arr = new int[5];
            // initialize the first
            // element of the array
            arr[0] = 10;
            // initialize the second
            // element of the array
            arr[1] = 20;
            // so on...
            arr[2] = 30;
            arr[3] = 40;
            arr[4] = 50;
            // accessing the elements
            // of the specified array
            for (int i = 0; i < arr.length; i++)
                    "Element at index "
                    + i + " : " + arr[i]);
    Element at index 0 : 10
    Element at index 1 : 20
    Element at index 2 : 30
    Element at index 3 : 40
    Element at index 4 : 50

    Problem with above Array implementation:
    Once we create the Array we cannot alter the size of the array. So the size of the array is unalterable.

Dynamic Data Structure

In Dynamic data structure, the size of the structure is not fixed and can be modified during the operations performed on it. Dynamic data structures are designed to facilitate change of data structures in the run time.

Examples of Dynamic Data Structures:

  1. Singly Linked List

    Linked List are linear data structures where the elements are not stored in contiguous locations and every element is a separate object with a data part and address part. The elements are linked using pointers and addresses. Each element is known as a node. Due to the dynamicity and ease of insertions and deletions, they are preferred over the arrays.

    // Java code for Linked List implementation
    import java.util.*;
    public class Test {
        public static void main(String args[])
            // Creating object of class linked list
            LinkedList<String> object
                = new LinkedList<String>();
            // Adding elements to the linked list
            object.add(2, "E");
            System.out.println("Linked list : "
                               + object);
            // Removing elements from the linked list
                "Linked list after deletion: "
                + object);
            // Finding elements in the linked list
            boolean status = object.contains("E");
            if (status)
                    "List contains the element 'E' ");
                    "List doesn't contain the element 'E'");
            // Number of elements in the linked list
            int size = object.size();
                "Size of linked list = " + size);
            // Get and set elements from linked list
            Object element = object.get(2);
                "Element returned by get() : "
                + element);
            object.set(2, "Y");
                "Linked list after change : "
                + object);
    Linked list : [D, A, E, B, C, F, G]
    Linked list after deletion: [A, E, F]
    List contains the element 'E' 
    Size of linked list = 3
    Element returned by get() : F
    Linked list after change : [A, E, Y]
  2. Doubly Linked List

    A Doubly Linked List (DLL) contains an extra pointer, typically called the previous pointer, together with next pointer and data which are there in a singly linked list.

    // Java program to demonstrate DLL
    // Class for Doubly Linked List
    public class DLL {
        Node head; // head of list
        /* Doubly Linked list Node*/
        class Node {
            int data;
            Node prev;
            Node next;
            // Constructor to create a new node
            // next and prev is by default
            // initialized as null
            Node(int d) { data = d; }
        // Adding a node at the front of the list
        public void push(int new_data)
            /* 1. allocate node 
            * 2. put in the data */
            Node new_Node = new Node(new_data);
            /* 3. Make next of new node as head
            and previous as NULL */
   = head;
            new_Node.prev = null;
            /* 4. change prev of head node to new node */
            if (head != null)
                head.prev = new_Node;
            /* 5. move the head to point to the new node */
            head = new_Node;
        /* Given a node as prev_node, 
        insert a new node after the given node */
        public void InsertAfter(
            Node prev_Node, int new_data)
            /*1. check if the given
            prev_node is NULL */
            if (prev_Node == null) {
                    "The given previous node"
                    + " cannot be NULL ");
            /* 2. allocate node 
            * 3. put in the data */
            Node new_node = new Node(new_data);
            /* 4. Make next of new node 
            as next of prev_node */
            /* 5. Make the next of
            prev_node as new_node */
   = new_node;
            /* 6. Make prev_node as
            previous of new_node */
            new_node.prev = prev_Node;
            /* 7. Change previous of 
            new_node's next node */
            if ( != null)
       = new_node;
        // Add a node at the end of the list
        void append(int new_data)
            /* 1. allocate node 
            * 2. put in the data */
            Node new_node = new Node(new_data);
            Node last = head; /* used in step 5*/
            /* 3. This new node is going
            to be the last node, so 
            * make next of it as NULL*/
   = null;
            /* 4. If the Linked List is empty,
            * then make the new 
            * node as head */
            if (head == null) {
                new_node.prev = null;
                head = new_node;
            /* 5. Else traverse till
            the last node */
            while ( != null)
                last =;
            /* 6. Change the next of last node */
   = new_node;
            /* 7. Make last node as
            previous of new node */
            new_node.prev = last;
        // This function prints contents
        // of linked list starting
        // from the given node
        public void printlist(Node node)
            Node last = null;
                "Traversal in forward Direction");
            while (node != null) {
                System.out.print( + " ");
                last = node;
                node =;
                "Traversal in reverse direction");
            while (last != null) {
                System.out.print( + " ");
                last = last.prev;
        /* Driver program to test above functions*/
        public static void main(String[] args)
            /* Start with the empty list */
            DLL dll = new DLL();
            // Insert 6. So linked list becomes 6->NULL
            // Insert 7 at the beginning.
            // So linked list becomes 7->6->NULL
            // Insert 1 at the beginning.
            // So linked list becomes 1->7->6->NULL
            // Insert 4 at the end.
            // So linked list becomes
            // 1->7->6->4->NULL
            // Insert 8, after 7.
            // So linked list becomes
            // 1->7->8->6->4->NULL
            dll.InsertAfter(, 8);
            System.out.println("Created DLL is: ");
    Created DLL is: 
    Traversal in forward Direction
    1 7 8 6 4 
    Traversal in reverse direction
    4 6 8 7 1
  3. Vector

    The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. Vector implements a dynamic array that means it can grow or shrink as required. Like an array, it contains components that can be accessed using an integer index.

    // Java code illustrating Vector data structure
    import java.util.*;
    class Vector_demo {
        public static void main(String[] arg)
            // Create default vector
            Vector v = new Vector();
            System.out.println("Vector is " + v);


    Vector is [1, 2, geeks, forGeeks, 3]
  4. Stack

    Java Collection framework provides a Stack class which models and implements a Stack data structure. The class is based on the basic principle of last-in-first-out. In addition to the basic push and pop operations, the class provides three more functions of empty, search and peek. The class can also be said to extend Vector and treats the class as a stack with the five mentioned functions. The class can also be referred to as the subclass of Vector.

    // Java code for stack implementation
    import java.util.*;
    public class stack_implementation {
        public static void main(String a[])
            Stack<Integer> stack = new Stack<>();
            int n = stack.size();
            for (int i = 0; i < n; i++) {


    Related articles:

  5. Queue

    The Queue interface is available in java.util package and extends the Collection interface. The queue collection is used to hold the elements about to be processed and provides various operations like the insertion, removal etc. It is an ordered list of objects with its use limited to insert elements at the end of the list and deleting elements from the start of list i.e. it follows the FIFO or the First-In-First-Out principle.

    // Java orogram to demonstrate working
    // of Queue interface in Java
    import java.util.LinkedList;
    import java.util.Queue;
    public class QueueExample {
        public static void main(String[] args)
            Queue<Integer> q = new LinkedList<>();
            // Adds elements {0, 1, 2, 3, 4} to queue
            for (int i = 0; i < 5; i++)
            // Display contents of the queue.
            System.out.println("Elements of queue-" + q);
            // To remove the head of queue.
            int removedele = q.remove();
            System.out.println("removed element-"
                               + removedele);
            // To view the head of queue
            int head = q.peek();
            System.out.println("head of queue-"
                               + head);
            // Rest all methods of collection interface,
            // Like size and contains can be
            // used with this implementation.
            int size = q.size();
            System.out.println("Size of queue-"
                               + size);
    Elements of queue-[0, 1, 2, 3, 4]
    removed element-0
    [1, 2, 3, 4]
    head of queue-1
    Size of queue-4

    Related articles:

  6. Tree

    A Tree is a data structure that stores values inside entities called Nodes. Nodes are connected through lines referred to as edges. Each node stores a value inside it.

    • Root is the topmost node of the tree.
    • Parent is a node that has one or more Nodes attached to it.
    • Edge is the link joining the two nodes.
    • Child is a node that has a parent node
    • Leaf is a node that doesn’t have any child node attached to it, it is the bottommost node of a tree.

    // Java program for different tree traversals
    /* Class containing left and right child of current 
    node and key value*/
    class Node {
        int key;
        Node left, right;
        public Node(int item)
            key = item;
            left = right = null;
    class BinaryTree {
        // Root of Binary Tree
        Node root;
            root = null;
        /* Given a binary tree, print
        its nodes according to the 
        "bottom-up" postorder traversal. */
        void printPostorder(Node node)
            if (node == null)
            // first recur on left subtree
            // then recur on right subtree
            // now deal with the node
            System.out.print(node.key + " ");
        /* Given a binary tree, 
        print its nodes in inorder*/
        void printInorder(Node node)
            if (node == null)
            /* first recur on left child */
            /* then print the data of node */
            System.out.print(node.key + " ");
            /* now recur on right child */
        /* Given a binary tree,
        print its nodes in preorder*/
        void printPreorder(Node node)
            if (node == null)
            /* first print data of node */
            System.out.print(node.key + " ");
            /* then recur on left sutree */
            /* now recur on right subtree */
        // Wrappers over above recursive functions
        void printPostorder() { printPostorder(root); }
        void printInorder() { printInorder(root); }
        void printPreorder() { printPreorder(root); }
        // Driver method
        public static void main(String[] args)
            BinaryTree tree = new BinaryTree();
            tree.root = new Node(1);
            tree.root.left = new Node(2);
            tree.root.right = new Node(3);
            tree.root.left.left = new Node(4);
            tree.root.left.right = new Node(5);
                "Preorder traversal of binary tree is ");
                "\nInorder traversal of binary tree is ");
                "\nPostorder traversal of binary tree is ");
    Preorder traversal of binary tree is
    1 2 4 5 3 
    Inorder traversal of binary tree is
    4 2 5 1 3 
    Postorder traversal of binary tree is
    4 5 2 3 1

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.  To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

In case you wish to attend live classes with industry experts, please refer Geeks Classes Live 

My Personal Notes arrow_drop_up
Recommended Articles
Page :