An array is a group of variables of the same data type and referred to by a common name. An array is contiguous block of memory locations referred by a common name.

For e.g.

To store the marks of 5000 students, you can declare an array, marks, of size 5000 and can store the marks of as many students.

int marks[] = new int[5000];


Why array is needed?

You might come across a situation where you need to store similar type of values for a large number of data items.

For e.g.

To store the marks of all the students of a university, you need to declare thousands of variables. In addition, each variable name needs to be unique. To avoid such situations, you can use arrays.

An array consists of a name and the number of elements of the array. You can refer to a specific array element by the array name and the element number, which is known as the index number.

Note: – Array index element number always starts with 0(zero).

Simple Java Application using Arrays

class ArrayDemo {
     public static void main(String[] args) {
          int[] anArray;              // declares an array of integers

          anArray = new int[10];      // allocates memory for 10 integers

          anArray[0] = 100; // initialize first element
          anArray[1] = 200; // initialize second element
          anArray[2] = 300; // etc.
          anArray[3] = 400;
          anArray[4] = 500;
          anArray[5] = 600;
          anArray[6] = 700;
          anArray[7] = 800;
          anArray[8] = 900;
          anArray[9] = 1000;

          System.out.println("Element at index 0: " + anArray[0]);
          System.out.println("Element at index 1: " + anArray[1]);
          System.out.println("Element at index 2: " + anArray[2]);
          System.out.println("Element at index 3: " + anArray[3]);
          System.out.println("Element at index 4: " + anArray[4]);
          System.out.println("Element at index 5: " + anArray[5]);
          System.out.println("Element at index 6: " + anArray[6]);
          System.out.println("Element at index 7: " + anArray[7]);
          System.out.println("Element at index 8: " + anArray[8]);
          System.out.println("Element at index 9: " + anArray[9]);

The output from this program is:

Element at index 0: 100
Element at index 1: 200
Element at index 2: 300
Element at index 3: 400
Element at index 4: 500
Element at index 5: 600
Element at index 6: 700
Element at index 7: 800
Element at index 8: 900
Element at index 9: 1000

Declaring a Variable to Refer to an Array

The above program declares anArray with the following line of code:

int[] anArray;          // declares an array of integers

Like declarations for variables of other types, an array declaration has two components: the array’s type and the array’s name. An array’s type is written as type[], where type is the data type of the contained elements; the square brackets are special symbols indicating that this variable holds an array. The size of the array is not part of its type (which is why the brackets are empty). An array’s name can be anything you want, provided that it follows the rules and conventions as previously discussed in the naming section. As with variables of other types, the declaration does not actually create an array — it simply tells the compiler that this variable will hold an array of the specified type.

Similarly, you can declare arrays of other types:

byte[] anArrayOfBytes;
short[] anArrayOfShorts;
long[] anArrayOfLongs;
float[] anArrayOfFloats;
double[] anArrayOfDoubles;
boolean[] anArrayOfBooleans;
char[] anArrayOfChars;
String[] anArrayOfStrings;

You can also place the square brackets after the array’s name:

float anArrayOfFloats[]; // this form is discouraged

However, convention discourages this form; the brackets identify the array type and should appear with the type designation.

Creating, Initializing, and Accessing an Array

One way to create an array is with the new operator. The next statement in the ArrayDemo program allocates an array with enough memory for ten integer elements and assigns the array to the anArray variable.

anArray = new int[10];  // create an array of integers

If this statement were missing, the compiler would print an error like the following, and compilation would fail: Variable anArray may not have been initialized.

The next few lines assign values to each element of the array:

anArray[0] = 100; // initialize first element
anArray[1] = 200; // initialize second element
anArray[2] = 300; // etc.

Each array element is accessed by its numerical index:

System.out.println("Element 1 at index 0: " + anArray[0]);
System.out.println("Element 2 at index 1: " + anArray[1]);
System.out.println("Element 3 at index 2: " + anArray[2]);

Alternatively, you can use the shortcut syntax to create and initialize an array:

int[] anArray = {100, 200, 300, 400, 500, 600, 700, 800, 900, 1000};

Here the length of the array is determined by the number of values provided between { and }.You can also declare an array of arrays (also known as a multidimensional array) by using two or more sets of square brackets, such as String[][] names. Each element, therefore, must be accessed by a corresponding number of index values.

In the Java programming language, a multidimensional array is simply an array whose components are themselves arrays. This is unlike arrays in C or Fortran. A consequence of this is that the rows are allowed to vary in length, as shown in the following MultiDimArrayDemo program:

class MultiDimArrayDemo {
    public static void main(String[] args) {
        String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
                            {"Smith", "Jones"}};
        System.out.println(names[0][0] + names[1][0]); //Mr. Smith
        System.out.println(names[0][2] + names[1][1]); //Ms. Jones

The output from this program is:

    Mr. Smith
    Ms. Jones

Finally, you can use the built-in length property to determine the size of any array. The code


will print the array’s size to standard output.

Advantages of using Arrays

  1. You can refer to a large number of elements by just specifying the index number and the array name.
  2. Arrays make it easy to do calculations in a loop.

The various types of arrays in java are:

  • One-dimensional arrays
  • two-dimensional arrays

One-dimensional Arrays

One-dimensional array is a list of variables of the same data type.

Syntax to declare a one-dimensional array

type array_name []; //type is the datatype of the array.

For e.g.

String designations []; // designations is name of the array.

Allocating Memory to Arrays

The new operator is used to allocate memory to an array.

Syntax to allocate memory

array_name = new type[size];

For e.g.

designations = new String[10]; //size of the array is 10.

Two-dimensional Arrays

In additions to one-dimensional arrays, you can create two-dimensional arrays. To declare two-dimensional arrays, you need to specify multiple square brackets after the array name.

Syntax to declare a two dimensional array

type array_name = new type[rows][cols];

For e.g.

int multidim[] = new int[3][];

In a two-dimensional array,

  1. You need to allocate memory for only the first dimension.
  2. You can allocate the remaining dimensions separately.
  3. When you allocate memory to the second dimension, you can also allocate different number to each dimension.

For e.g.

int multidim[] = new int[3][];
multidim[0] = new int[1];
multidim[1] = new int[4];


Accessing Arrays

You need to access various elements of an array to assign, retrieve, and manipulate the values stored in the array.

Assigning values to the Elements of an Array

To access a specific array,

  1. You need to specify the name of the array and the index number of the element.
  2. The index position of the first element in the array is 0.


For e.g.

String designations[];
designations = new String[2];
designations[0] = “General Manager”;
designations[1]=”Managing Director”;

You can declare and allocate memory to a user-defined array in a single statement.


type arr [] = new type[size];
For e.g.
int employees[] = new int[10];

You can also declare and initialize arrays in the same statement.

For e.g.
String designations[] = {“General Manager”, ”Managing Director”};

Accessing values from various Elements of an Array

You can access values from elements in the array by referring to the element by its index number.

For e.g.

String designations[];
designations = new String[3];
designations[1] = “General Manager”;
designations[2]=”Managing Director”;

In the above example, the value of the third element of the array is assigned to the first element of the array.


Tags: ,

One Response to “Array”

  1. richardbernat Says:

    If you have questions, pls post it here

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: