Declaring an array in java

Posted By: Java Examples - 7:08 PM

Share

& Comment

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 wordFrequencyTable because the word "table" suggests many entries. If you had an array of single words, you might call it words or wordList. 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.
/**
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package array;

import java.util.Scanner;

/**
 *
 * @author ACHCHUTHAN
 */
public class InputArray {

    private static Scanner input = new Scanner(System.in);
    private static int[] array = new int[10];

    public void printArray() {
        System.out.println("Elements of the array : ");
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i] + "  ");
        }
    }

    public static void main(String[] args) {
        InputArray a = new InputArray();
        System.out.println("Enter the array elements :");
        for (int j = 0; j < array.length; j++) {
        System.out.println(j+" Element : ");    
            array[j] = input.nextInt();
        }
        a.printArray();
    }
}
Output of this program :

run:
Enter the array elements :
0 Element : 
56
1 Element : 
98
2 Element : 
89
3 Element : 
65
4 Element : 
32
5 Element : 
65
6 Element : 
65
7 Element : 
896
8 Element : 
56
9 Element : 
23
Elements of the array : 
56  98  89  65  32  65  65  896  56  23  
BUILD SUCCESSFUL (total time: 14 seconds)

About Java Examples

I’m passionate about Web Development and Programming and I go to extreme efforts to meet my passion. I’m a believer of learning the fundamentals first. I try to understand everything little bit more than the average.

Copyright © 2016 Java Examples ACHCHUTHAN.ORG. Designed by Templateism .