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