I come from a php background and in php, there is an array_size()
function which tells you how many elements in the array are used.
Is there a similar
Also, it's probably useful to note that if you have a multiple dimensional Array, you can get the respective dimension just by appending a '[0]' to the array you are querying until you arrive at the appropriate axis/tuple/dimension.
This is probably better explained with the following code:
public class Test {
public static void main(String[] args){
String[][] moo = new String[5][12];
System.out.println(moo.length); //Prints the size of the First Dimension in the array
System.out.println(moo[0].length);//Prints the size of the Second Dimension in the array
}
}
Which produces the output:
5
12
Yes, .length
(property-like, not a method):
String[] array = new String[10];
int size = array.length;
array.length
It is actually a final member of the array, not a method.
array.length final property
it is public and final property. It is final because arrays in Java are immutable by size (but mutable by element's value)
All the above answers are proper. The important thing to observe is arrays have length attribute but not length method. Whenever you use strings and arrays in java the three basic models you might face are:
In java there is a length
field that you can use on any array to find out it's size:
String[] s = new String[10];
System.out.println(s.length);