BeginnersBook

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java program to print number of elements in an array

By Chaitanya Singh | Filed Under: Java Examples

In this tutorial, you will learn how to find the number of elements present in an array.

Illustration:

Array: {1, 2, 3, 4, 5}
Number of elements  = count of the elements  = 5

Example: Program to print number of elements in an array

In this example, we have two arrays. We are finding the count of the elements in both the arrays. The steps to count the number of elements is same in any type of array, you can simply use .length property of the array to quickly find the size of the array.

public class JavaExample {
  public static void main(String[] args) {
    //Initializing an int array
    int [] numbers = new int [] {2, 4, 6, 8, 10, 12};
    // We can use length property of array to find the count of elements
    System.out.println("Number of elements in the given int array: " + numbers.length);

    //Initializing a String array
    String [] names = new String [] {"Rick", "Luna", "Steve", "John"};
    System.out.println("Number of elements in the given String array: " + names.length);
  }
}

Output:

Number of elements in the given int array: 6
Number of elements in the given String array: 4

Recommended Java Programs

  1. Java program to find smallest number in an array
  2. Java program to find the largest element of an array
  3. Java program to print the duplicate elements of an array
❮ Java TutorialJava Programs ❯

Programs

  • C Programs
  • Java Programs

Java Examples

  • Check Odd-even
  • Linear Search
  • Binary Search
  • Floyd's Triangle
  • Reverse number
  • Random Number
  • first n prime numbers
  • Disp prime Numbers
  • Check Prime number
  • Palindrome String
  • Find factorial
  • Sum of elements of Array
  • Area of rectangle
  • Area of Square
  • Area of Triangle
  • Circle

Tutorials

  • Java Tutorial
  • OOPs Concepts
  • Java String
  • Exception handling
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap