How to determine length or size of an Array in Java?
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 One)
The naive method is to use for loop to determine size/length of char, integer and string type of arrays.
Below is the implementation:
Java
// Java program to demonstrate for loop // to calculate size/length of all type of arrays import java.util.*; public class Main { public static void main(String[] argv) { // Creating Arrays and Populating them 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 ; // print char array System.out.print( "Char Array: [ " ); for ( char c : char_arr) { System.out.print( "'" + c + "' " ); ci++; } System.out.println( "]" ); // print integer array System.out.print( "Integer Array: [ " ); for ( int c : int_arr) { System.out.print(c + " " ); ii++; } System.out.println( "]" ); // print string array System.out.print( "String Array: [ " ); for (String c : str_arr) { System.out.print( "'" + c + "' " ); si++; } System.out.println( "]\n" ); // print the size/length of all arrays System.out.println( "Size of char array = " + ci); System.out.println( "Size of integer array = " + ii); System.out.println( "Size of string array = " + si); } } // This code is contributed by Susobhan Akhuli |
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
Time Complexity: O(N), where N is the size of the array.
Auxiliary Space: O(1)
Method 2:
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 array[] in Java using length variable:
Example 1:
Java
// Java program to illustrate // how to get the length of the array public class Test { public static void main(String[] args) { // Here array is the // array name of int type int [] array = new int [ 4 ]; System.out.println( "The size of " + "the array is " + array.length); } } |
The size of the array is 4
Time Complexity: O(1)
Auxiliary Space: O(1)
Example 2:
Java
// Java program to illustrate // how to get the length of the array public class Test { public static void main(String[] args) { // Here str is the array name // of String type. String[] str = { "GEEKS" , "FOR" , "GEEKS" }; System.out.println( "The size of " + "the array is " + str.length); } } |
The size of the array is 3
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 3:
Alternatively, we can use the size() method of the java.util.ArrayList class, which returns the number of elements in the list.
Below is the implementations:
Example 1:
Java
// Java program to demonstrate // size() method // for Integer value import java.util.*; public class GFG1 { public static void main(String[] argv) { // Creating object of ArrayList<Integer> ArrayList<Integer> arrlist = new ArrayList<Integer>(); // Populating arrlist1 arrlist.add( 1 ); arrlist.add( 2 ); arrlist.add( 3 ); arrlist.add( 4 ); arrlist.add( 5 ); // print arrlist System.out.println( "Array: " + arrlist); // getting total size of arrlist // using size() method int size = arrlist.size(); // print the size of arrlist System.out.println( "Size of array = " + size); } } // This code is contributed by Susobhan Akhuli |
Array: [1, 2, 3, 4, 5] Size of array = 5
Time Complexity: O(1)
Auxiliary Space: O(1)
Example 2:
Java
// Java program to demonstrate // size() method // for String value import java.util.*; public class GFG1 { public static void main(String[] argv) { // Creating object of ArrayList<Integer> ArrayList<String> arrlist = new ArrayList<String>(); // Populating arrlist1 arrlist.add( "GFG" ); arrlist.add( "GEEKS" ); arrlist.add( "GEEKSFORGEEKS" ); // print arrlist System.out.println( "Array: " + arrlist); // getting total size of arrlist // using size() method int size = arrlist.size(); // print the size of arrlist System.out.println( "Size of array = " + size); } } // This code is contributed by Susobhan Akhuli |
Array: [GFG, GEEKS, GEEKSFORGEEKS] Size of array = 3
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 4:
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:
Java
// Java program to demonstrate Stream.count() method // to calculate size/length of different arrays import java.util.*; public class Main { public static void main(String[] argv) { // Creating Array and Populating them int [] int_arr = { 1 , 2 , 3 , 4 , 5 , 6 , 7 }; String[] str_arr = { "GFG" , "GEEKS" , "GEEKSFORGEEKS" }; // print integer array System.out.println( "Integer Array: " + Arrays.toString(int_arr)); // print string array System.out.println( "String Array: " + Arrays.toString(str_arr) + "\n" ); // calculating the size/length of the arrays long ii = Arrays.stream(int_arr).count(); long si = Arrays.stream(str_arr).count(); // print the size/length of the arrays System.out.println( "Size of integer array = " + ii); System.out.println( "Size of string array = " + si); } } // This code is contributed by Susobhan Akhuli |
Integer Array: [1, 2, 3, 4, 5, 6, 7] String Array: [GFG, GEEKS, GEEKSFORGEEKS] Size of integer array = 7 Size of string array = 3
Time Complexity: O(1)
Auxiliary Space: O(1)
Method 5:
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:
Java
// Java program to demonstrate length() method // to calculate size/length of only char array import java.util.*; public class Main { public static void main(String[] argv) { // Creating Array of character and Populating them String char_arr = "GEEKSFORGEEKS" ; // print char array System.out.println( "Char Array: " + char_arr); // calculating the size/length of the array int ci = char_arr.length(); // print the size/length of the array System.out.println( "Size of integer array = " + ci); } } // This code is contributed by Susobhan Akhuli |
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:
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:
Java
// Java program to demonstrate Collection.size() method // to calculate size/length of array import java.util.Collection; import java.util.HashSet; public class Main { public static void main(String[] argv) { // Creating collection Collection<Integer> collection = new HashSet<>(); // Populating them collection.add( 1 ); collection.add( 2 ); collection.add( 3 ); collection.add( 4 ); collection.add( 5 ); collection.add( 6 ); collection.add( 7 ); // print it System.out.println( "Array: " + collection); // calculating the size/length of the array int ii = collection.size(); // print the size/length of the array System.out.println( "Size of array = " + ii); } } // This code is contributed by Susobhan Akhuli |
Array: [1, 2, 3, 4, 5, 6, 7] Size of array = 7
Method 7:
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:
Java
// Java program to demonstrate Stream.count() method // to calculate size/length of different arrays import java.util.*; public class GFG { public static void main(String[] argv) { // Creating String Array String[] str_arr = { "GFG" , "GEEKS" , "GEEKSFORGEEKS" }; // print string array System.out.println( "String Array: " + Arrays.toString(str_arr) + "\n" ); // calculating the size/length of the array long si = Arrays.asList(str_arr).size(); // print the size/length of the array System.out.println( "Size of string array = " + si); } } // This code is contributed by Susobhan Akhuli |
String Array: [GFG, GEEKS, GEEKSFORGEEKS] Size of string array = 3
Please Login to comment...