Find the size of an array in java - codeyourslf
Do you want to find out the number of elements present in an array ? In this article I will discuss how to find the size of array in java.
Here I also used my android phone to write the code. I have used java android ide i.e java N-IDE. Please support.
Approach:-1 "find the length of int array in java"
public class usinglength {
public static void main(String args[]){
int[] arr = {4,6,2,7,4,7};
int len = arr.length;
System.out.println("The length of the arr is: "+ len);
}
}
Finding the length of an array means to find out the number of elements present in that array.
To find out the length of a string, we have length() method. But this length() doesn't work for array.
Here length variable is applicable to find the size of an array.
Here inside len variable I have stored the value of size, then printed it to the screen.
Output
Approach:-2 "find the size of a string or char array in java"
public class strirngLength {
public static void main(String[] args){
String[] name = {"Miku", "Rahul", "Raju", "Ram"};
char[] ch = {'a', 'e', 'i', 'o', 'u'};
System.out.println("The length of string array is: "+name.length);
System.out.println("The length of char array is: "+ch.length);
}
}
Here the concept is same, but I have not create any extra variable to store the value of length.
Directly I have printed the size of that Array.
Output
Hey if you have any questions or any query, please ask me in the comment below, I will definitely answer to your questions.
And please share this article with your friends, as a result they also can learn effectively.