Declaring an Array in Java


In this tutorial, you will learn to work with arrays in Java. You will learn to declare, initialize, and access array elements with the help of examples.

An array variable is like other variables -- you must declare it, which means you must declare the type of elements that are in an array. All elements must be the same type. Write the element type name, then "[ ]", then the name of the array variable. The declaration allocates only enough space for a reference to an array (typically 4 bytes), but doesn't create the actual array object.

String [ ] args; // args is an array of Strings
int [ ] scores; // scores is an array of ints
JButton [ ] controlButtons; // controlButtons is an array of J Buttons


No size in declaration. Unlike some languages, never put the size of the array in the declaration because an array declaration specifies only the element type and the variable name. The size is specified when you allocate space for the array.

Names - Plurals or collective nouns are most common for array names :

Most programming guidelines suggest using plural names, or nouns denoting a collection of things, for arrays and other collections of multiple values. This is not a rigid rule, and your choice will often be based on linguistic sensitives. For example, you might have an array of words and their frequencies. It would be entirely appropriate to name this word Frequency Table because the word "table" suggests many entries. If you had an array of single words, you might call it words or word List. Naming the array using the singular word would probably be very confusing to most readers.
Examples in this text often follow the common convention of using the array variable name "a". This seriously violates the rule of having meaningful names, so please don't adopt this textbook-example style in your code!

Allocate an array object with new :

Create an array using new. This example creates an array of 100 int elements, from a[0] to a[99].
int [ ] a; // Declare a to be an array of ints
a = new int[100]; // Allocate an array of 100 ints

These are often combined in one line.
int [ ] a = new int[100]; // Declare and allocate.


Length of an array :

Each array has a constant (final) instance variable that has its length. You can find out how many elements an array can hold by writing the array name followed by length. In the previous example, a.length would be 100. Remember that this is the number of elements in the array, one more than the maximum subscript.

Example - Reading into an array :

This shows typical input code for reading into an array from a Scanner. This program is very strange because it doesn't do anything with the data, but it's shown here as an early step in the iterative process.

Declaring an Array


When you run the program, the output will be:

Enter the array Elements : 
 Element  0 : 58
 Element  1 : 65
 Element  2 : 56
 Element  3 : 23
 Element  4 : 14
Elements of an Array : 
58  65  56  23  14  

أحدث أقدم