Opening Hours :7AM to 9PM
class countElement { public int countElements(int[] arr) { int count = arr.length; return count; } } public class Main { public static void main(String[] args) { countElement ce=new countElement(); int[] arr = {1, 5, 3, 8, 2, 9, 4}; int count =ce. countElements(arr); System.out.println("The number of elements in the array is: " + count); } }This program uses the property arr.length to find the number of elements present in the array. The length property returns an integer value that represents the number of elements present in the array.
class countElement { public int countElements(int[] arr) { int count = 0; for (int i = 0; i < arr.length; i++) { count++; } return count; } } public class Main { public static void main(String[] args) { countElement ce=new countElement(); int[] arr = {1, 5, 3, 8, 2, 9, 4}; int count = ce.countElements(arr); System.out.println("The number of elements in the array is: " + count); } }In both examples above, the output will be: