Saturday, 27 December 2014

How to See the Length of an Array in Java

The simple example below creates an array of names then uses the .length method to see how long it is:

andrew@UBUNTU:~/Java$ cat prog56.java
public class prog56
{
public static void main (String args[])
  {
  String[] names =
  {"Andrew", "Brian", "Colin"};
  System.out.println
  ("names has " + names.length + " entries");
  }
}
andrew@UBUNTU:~/Java$ javac prog56.java
andrew@UBUNTU:~/Java$ java prog56
names has 3 entries
andrew@UBUNTU:~/Java$