Home » Java programming language

Java LinkedList int size() method with Example

Java LinkedList int size() method: Here, we are going to learn about the int size() method of LinkedList class with its syntax and example.
Submitted by Preeti Jain, on June 24, 2019

LinkedList int size() method

  • This method is available in package java.util.Collection and here, Collection is an interface.
  • This method is declared in interface Collection and implemented by the LinkedList Class.
  • This method is used to check how many elements exist in the Linked list or return the size of the Linked list.

Syntax:

    int size(){
    }

Parameter(s):

This method does not accept any parameter of the Linked List.

Return value:

The return type of this method is int that means this method returns the size of the list or number of elements exists in the LinkedList.

Java program to demonstrate example of LinkedList size() method

import java.util.LinkedList;

public class LinkList {
    public static void main(String[] args) {

        // Create an object of linked list 
        LinkedList list = new LinkedList();

        // use add() method to add few elements in the linked list 
        list.add(10);
        list.add(20);
        list.add(30);
        list.add(40);
        list.add(50);

        //  Current Linked list Output
        System.out.println("The  Current Linked List is :" + list);

        // With the help of size() we will check 
        // how many elements exists in the linked list. 

        System.out.println("The size() of the list is  : " + list.size());
    }
}

Output

D:\Programs>javac LinkList.java

D:\Programs>java LinkList
The  Current Linked List is :[10, 20, 30, 40, 50]
The size() of the list is  : 5


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.