Computer programming

Computer programming

Computer programming news and links

Java tutorial Array length

Java tutorial Array length

In order to get the number of elements in an array, you can use the length field of an array. The length field of an array returns the size of the array. It can be used by writing,

arrayName.length

For example, given the previous example, we can re-write it as,

public class ArraySample
{
public static void main( String[] args ){

int[] ages = new int[100];

for( int i=0; i
  1. When creating for loops to process the elements of an array, use the array object's length field in the condition statement of the for loop. This will allow the loop to adjust automatically for different-sized arrays.
  2. Declare the sizes of arrays in a Java program using named constants to make them easy to change. For example,
final int ARRAY_SIZE = 1000; //declare a constant
. . .
int[] ages = new int[ARRAY_SIZE];

Related Post:

Java Tutorial switch statement source code

Java tutorial source code get input from keyboard

Java Tutorial Using JOptionPane to get input

Java tutorial Decision Control Structures: if statement

PHP Tutorial

Java programing source code sample printing characters

Java tutorial if-else statement

C programming sample source code: Sum of 2 numbers

Java Programming Features

Sponsors
Comments
Be the first to leave a comment!
Add a Comment:
Already a member? Log In
Sponsors
About the Author

9 Kudos
Top Geek Articles
Celebrities on the Phone
Cell phones are to celebrities like bats are to baseball: no one runs too far without them.
Why every guy should buy their girlfriend Wii Fit.
Gratuitous...
Hot Geeks -- The Sexiest Geeky Girls
These girls are gorgeous AND they'll play Warcraft with you. Doesn't get much better than that.
More From Zimbio
Copyright © 2009 - Zimbio, Inc. Some rights reserved.