How to find the size/length of an array in java [2 ways] - CY

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.

Find the size of an array in java - codeyourslf

How to find the size/length of an array in java [2 ways] - CY

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);

 }

}

 
+
Explanation

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

find the length of int array in java

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);

    }

}

  
+
Explanation

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

find the size of a string or char array in java

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.

`

Post a Comment

Hey Please 👏, feel free to share your opinion 🌍
© codeYourslf. All rights reserved. Distributed by Pixabin