How to find length or size of an Array in Java?
Last Updated :
26 Feb, 2024
In Java, an array is a data structure that stores a fixed-size collection of elements of the same type. To determine the length or size of an array in Java, we can use different methods.
Method 1: Naive Approach to Find Java Array Length
The naive method is used for loop to determine the size/length of char, integer, and string type of arrays.
Below is the implementation of the above approach:
Java
import java.util.*;
public class Main {
public static void main(String[] argv)
{
char [] char_arr = { 'a' , 'b' , 'c' , 'd' , 'e' };
int [] int_arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 };
String[] str_arr
= { "GFG" , "GEEKS" , "GEEKSFORGEEKS" };
int ci = 0 , ii = 0 , si = 0 ;
System.out.print( "Char Array: [ " );
for ( char c : char_arr) {
System.out.print( "'" + c + "' " );
ci++;
}
System.out.println( "]" );
System.out.print( "Integer Array: [ " );
for ( int c : int_arr) {
System.out.print(c + " " );
ii++;
}
System.out.println( "]" );
System.out.print( "String Array: [ " );
for (String c : str_arr) {
System.out.print( "'" + c + "' " );
si++;
}
System.out.println( "]\n" );
System.out.println( "Size of char array = " + ci);
System.out.println( "Size of integer array = " + ii);
System.out.println( "Size of string array = " + si);
}
}
|
Output
Char Array: [ 'a' 'b' 'c' 'd' 'e' ]
Integer Array: [ 1 2 3 4 5 6 7 ]
String Array: [ 'GFG' 'GEEKS' 'GEEKSFORGEEKS' ]
Size of char array = 5
Size of integer array = 7
Size of string array = 3
The complexity of the above method
Time Complexity: O(N), where N is the size of the array.
Auxiliary Space: O(1)
Method 2: Using length() Method to find Java Array Size
There is a length field available in the array that can be used to find the length or size of the array.
array.length: length is a final variable applicable for arrays. With the help of the length variable, we can obtain the size of the array.
Examples:
int size = arr[].length;
// length can be used
// for int[], double[], String[]
// to know the length of the arrays.
Below is the illustration of how to get the length of an array[] in Java using the length variable:
Example 1:
Java
public class Test {
public static void main(String[] args)
{
int [] array = new int [ 4 ];
System.out.println( "The size of "
+ "the array is "
+ array.length);
}
}
|
Output
The size of the array is 4
Example 2:
Java
public class Test {
public static void main(String[] args)
{
String[] str = { "GEEKS" , "FOR" , "GEEKS" };
System.out.println( "The size of "
+ "the array is " + str.length);
}
}
|
Output
The size of the array is 3
The complexity of the above method
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 3: Using size() to Find Java array size
Alternatively, we can use the size() method of the java.util.ArrayList class, which returns the number of elements in the list.
the Example 1:
Java
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
ArrayList<Integer> arrlist
= new ArrayList<Integer>();
arrlist.add( 1 );
arrlist.add( 2 );
arrlist.add( 3 );
arrlist.add( 4 );
arrlist.add( 5 );
System.out.println( "Array: " + arrlist);
int size = arrlist.size();
System.out.println( "Size of array = " + size);
}
}
|
Output
Array: [1, 2, 3, 4, 5]
Size of array = 5
Example 2:
Java
import java.util.*;
public class GFG1 {
public static void main(String[] argv)
{
ArrayList<String> arrlist = new ArrayList<String>();
arrlist.add( "GFG" );
arrlist.add( "GEEKS" );
arrlist.add( "GEEKSFORGEEKS" );
System.out.println( "Array: " + arrlist);
int size = arrlist.size();
System.out.println( "Size of array = " + size);
}
}
|
Output
Array: [GFG, GEEKS, GEEKSFORGEEKS]
Size of array = 3
The complexity of the above method
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 4: Using Stream API to check Java Array Length
Java 8 introduced the Stream API, which allows us to perform operations on arrays using functional programming. The count() method of the Stream class can be used to count the number of elements in an array.
Below is the implementation of the above approach:
Java
import java.util.*;
public class Main {
public static void main(String[] argv)
{
int [] int_arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 };
String[] str_arr
= { "GFG" , "GEEKS" , "GEEKSFORGEEKS" };
System.out.println( "Integer Array: "
+ Arrays.toString(int_arr));
System.out.println( "String Array: "
+ Arrays.toString(str_arr)
+ "\n" );
long ii = Arrays.stream(int_arr).count();
long si = Arrays.stream(str_arr).count();
System.out.println( "Size of integer array = " + ii);
System.out.println( "Size of string array = " + si);
}
}
|
Output
Integer Array: [1, 2, 3, 4, 5, 6, 7]
String Array: [GFG, GEEKS, GEEKSFORGEEKS]
Size of integer array = 7
Size of string array = 3
The complexity of the above method
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 5: Using length() method to Check Java Array length
The length() method is a method of the java.lang.String class which only returns the number of characters in the string, which is an array of characters. This method does not take any arguments and returns an int data type.
Below is the implementation of the above method:
Java
import java.util.*;
public class Main {
public static void main(String[] argv)
{
String char_arr = "GEEKSFORGEEKS" ;
System.out.println( "Char Array: " + char_arr);
int ci = char_arr.length();
System.out.println( "Size of integer array = " + ci);
}
}
|
Output
Char Array: GEEKSFORGEEKS
Size of integer array = 13
N.B.: The length variable is applicable to all types of array whereas the length() method is applicable only for string(array of characters) objects.
Method 6: Using the Collection size() method to find the Java Array size
The collection.size() method is a method of the java.util.Collection interface, which is implemented by many classes in the Java Collections Framework. This method returns the number of elements in the collection. The Collection interface is a root interface in the Java Collection Framework and it’s implemented by many classes such as ArrayList, LinkedList, HashSet, and TreeSet.
Below is the implementation of the above method:
Java
import java.util.Collection;
import java.util.HashSet;
public class Main {
public static void main(String[] argv)
{
Collection<Integer> collection = new HashSet<>();
collection.add( 1 );
collection.add( 2 );
collection.add( 3 );
collection.add( 4 );
collection.add( 5 );
collection.add( 6 );
collection.add( 7 );
System.out.println( "Array: " + collection);
int ii = collection.size();
System.out.println( "Size of array = " + ii);
}
}
|
Output
Array: [1, 2, 3, 4, 5, 6, 7]
Size of array = 7
Method 7: Converting Strings in the List to find the size
The Arrays.asList(myArray).size() method is used to return the size of an array when it is converted to a List. The size of the array is equal to the number of elements in the array.
Below is the implementation of the above method:
Java
import java.util.*;
public class GFG {
public static void main(String[] argv)
{
String[] str_arr
= { "GFG" , "GEEKS" , "GEEKSFORGEEKS" };
System.out.println( "String Array: "
+ Arrays.toString(str_arr)
+ "\n" );
long si = Arrays.asList(str_arr).size();
System.out.println( "Size of string array = " + si);
}
}
|
Output
String Array: [GFG, GEEKS, GEEKSFORGEEKS]
Size of string array = 3
Share your thoughts in the comments
Please Login to comment...