×

How to find the length of an Array in Java

Arrays:

An array is a sort of container object that stores constant quantities of values of a single type in one memory area. A finite number of items must all be of the same data type in order to store them in this data structure. Index-based access is offered by arrays, meaning that the first element of the array is stored at index 0, the second element is stored at index 1, and so on. It should be noted that arrays are also objects in Java, inheriting the Object class and implementing the interfaces. Any form of primitive value or object can be stored in an array. Java allows arrays in either a single dimension or several dimensions.

Array Declaration in Java:

datatype array[] = new datatype[size];

Example:

import java.io.*;
import java.util.*;
class Demo
{
     public static void main(String args[])
     {  
        Scanner sc = new Scanner(System.in);  
          int a[] = new int[5];
          for(int i=0; i<5; i++)  
        {  
              a[i]=sc.nextInt();  }  
           for (int i=0; i<5; i++)   
          {  
                 System.out.println(a[i]);  
           }  
      }
}

Output:

1 2 3 5 6
1
2
3
5
6

Length of an Array in Java:

In Java, an array is a data structure that holds numerous components of the same data type. Array length has a variety of uses in programming. The length of an array is important information to have while managing and performing various actions on it.

When we initialise an array, its length property is automatically assigned, and we may access it using Variable.length method.

datatype array[] = new datatype[size];
int a = array.length;

Example:

import java.io.*;
import java.util.*;
class Demo
{
     public static void main(String args[])
     {  
        Scanner sc = new Scanner(System.in);  
          int a[] = new int[5];
           int b = a.length;
           System.out.println(“Array size = “ + b);
          for(int i=0; i<a.length; i++)  
        {  
              a[i]=sc.nextInt();  }  
           for (int i=0; i<a.length; i++)   
          {  
                 System.out.println(a[i]);  
           }  
      }
}

Output:

9 6 3 8 5
Array size = 5
9
6
3
8
5

Length of Multidimensional Array in Java:

If we have a two-dimensional array named array, we can use array.length and array[index].length to determine the parent and child lengths.

Example:

import java.io.*;
import java.util.*;
class Length {
  public static void main(String args[]) {
    
    
    int array[][] = {{40,50,60},{70,80,90},{100,110,120}};
    System.out.print("array length = ");
    System.out.println(array.length);
    System.out.println("array[] length = " + array[0].length);
  }
}

Output:

array length = 3
array[] length = 3

Difference between Java Array Length vs length()

In Java, using rounded brackets at the end of a word denotes the calling of a method.

The length of an array in Java refers to the total number of elements it can contain. By using the dot operator and the array name, we can utilise this length attribute or variable to determine the size of an array in Java.

In Java, the final method length() is accessible to string object types. It can be used to determine how many characters are in a string.

Example:

import java.io.*;
import java.util.*;
 class Size{
    public static void main(String args[])
    {
        int[] a= new int[25];
        System.out.println("Array size = "+ a.length);
        String Str= "Javatpoint";
        System.out.println("String size = "+ Str.length());
    }
}

Output:

Array size = 25
String size = 10

Length of ArrayList in Java

The size() method of the Java class java.util.ArrayList can be used to determine the length or size of an ArrayList. The amount of entries in the array list is the size() method's return value, which is an integer. It is distinct from the array's length that serves as the ArrayList's backing array, which is referred to as the ArrayList's capacity. The default value of ArrayList is 10 when user did not specify the size of ArrayList. When the size (the number of elements in the array list) increases beyond a certain point, ArrayList automatically resizes because it is a growable array.

An ArrayList is also known as an empty ArrayList when it is first created, and size() will return zero in this case.

Example:

import java.io.*;
import java.util.*;


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


    ArrayList<String> names = new ArrayList<>();
    int size = names.size();
    System.out.println("Size of array list (before): " + size);


    names.add("David");
    names.add("Charle");
    names.add("Jack");
    size = names.size();


    System.out.println("Size of ArrayList (after): " + size);
    }
}

Output:

Size of array list (before): 0
Size of array list (after): 3

Related Topics

Java Char Keyword

The java char keyword is a data type which is defined as character data type.Char keyword belongs to a primitive data type where the data types are classified into primitive...

4 minutes read.

Java Boolean equals() method

The equals() method of Java Boolean class returns a Boolean value true if the specified argument is not null and is same as this object, else it returns false. Syntax public boolean...

2 minutes read.

Sphenic Number in Java

In this section, we will learn what is a sphenic number is and show you how to write Java programmes to determine if a specific number are sphenic or not....

3 minutes read.

Java Int Keyword

Among the primitive data types is the Java int keyword. To declare variables, use this. It can also be used with methods that return values of the integer type. It...

3 minutes read.

Transient variable in Java

In this article, you will be acknowledged about transient variable along with its functions. We would conclude by understanding an example program about it. Transient variable By introducing the transitory keyword, we...

3 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

GCD of Different SubSequences in Java

The positive numbers are provided in an array called inArr. The aim is to determine the number of distinct GCDs (Greatest Common Divisors) in each subsequence present in the input...

4 minutes read.

Figurate Number in Java

There have been several uses for figurate or figural numerals throughout history. A number that may be expressed by regular, distinct geometric shapes with spaced evenly points is referred to...

4 minutes read.

Java Linters

When it comes to programming, everyone makes mistakes. Errors are bad for developers since they are difficult to handle. But handling as many as possible errors will bring out the...

6 minutes read.

How to compare characters in Java

In this tutorial, we will learn about how to compare characters in Java. To compare characters in Java, we will learn about what is a character in Java Char The character is...

4 minutes read.

Pancake Sorting in Java

In the pancake sorting method, the array must be sorted using just one operation, which is: Flip the arrays arr at index 0 to index j by using flipArr(arr, h). Other sorting...

3 minutes read.

Getter and Setter Method in Java Example

In Java programming, getter and setter methods are often employed. The values of class fields can be accessed and changed using Java's getter and setter methods. A private access specifier...

6 minutes read.

Java this keyword

This Keyword in Java This keyword can be used in many different ways in Java. This is a reference variable in Java that points to the active object. In Java, the...

8 minutes read.

Producer Consumer Problem in Java Using Synchronized Block

The producer-consumer dilemma is a well-known instance of a multi-process synchronization issue in computing. Two processes—the producer and the consumer—are described in the issue, and they share a single, fixed-size...

4 minutes read.

How to Assign Static Value to Date in Java?

In this tutorial, we will learn certain ways to assign a static value to a date in java. We will see the limitation of each method that will lead to...

3 minutes read.

Java Integer compareUnsigned() method

The compareUnsigned() method of Integer class compares two int objects numerically by treating the values as unsigned. Syntax public static int compareUnsigned(int x , int y) Parameters The parameters ‘x’ and ‘y’ represent the...

1 minute read.

Minimum XOR value pair in Java

In this section, you will discuss about minimum XOR value pair in Java. The objective is to enforce a value that indicates the least XOR values of the two numbers from...

4 minutes read.

Check whether Java is installed or not

As we know that there are various operating systems, to check whether Java is installed or not in Windows and Mac we use the following ways.           Windows Operating System: There are several...

2 minutes read.

Program to check whether a given character is present in a string or not

In this article, you will understand the logic to find out whether the given character is present in the string or not and find out the position of the specified...

3 minutes read.

Best Java Libraries

One of the most widely used programming languages is Java. Java has a large number of libraries, including the standard Java library that includes libraries such as java.lang, java.util, and...

7 minutes read.