Array Introduction

An array is a collection of similar type of data types which has contiguous memory location. Array also known as static data structure because size of an array must be specified at the time of its declaration. Each item in an array is called an element, and each element is accessed by its numerical index because array in java is index based, the first element of the array is stored at the 0th index, 2nd element is stored on 1st index and so on.

we have to provide length of an array while array creation and once length is provide to the array then its length is fixed. we can not change the length of this array.

Arrays are objects in java and stores into heap memory. It allows to hold primitive values as well as object references.

Array can be single dimensional or multidimensional in Java.


Fig. Array

Features of Array

  • Arrays always indexed and index begins from 0.
  • Arrays are objects.
  • Array can hold data as well as reference of other objects.
  • Array occupies contiguous memory location.
  • Arrays are created during runtime.
  • Arrays are created in heap memory.
  • Array allows to access the elements randomly using index of element.
  • Array length once assigned then it fixed.

Array Declaration

In Java, we can declare an array with the following line of code:

				
 // declares an array of dataType
dataType[] arrayVariableName;

			

  • it can be primitive data types or java objects
  • it is an identifier

e.g.

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

			

Here anArray is an array that can hold integer values.

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 brackets after the array’s name:

we can also declare array using following line of code but below is not recommended:

				
// declares an array of dataType
dataType arrayVariableName[];

			

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

			

Java convention discourages above form.

Array Creation

In above example we have seen how to declare array but we haven’t defined how to create an array and how much elements this array can hold.

We can create array using below two methods.

1.) Creating array using new operator

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

			

Here,
anArray is an array. It can hold 10 values of int type. Please see below figure:

DataStructure Array Creation
Fig. Array Memory Allocation

In above figure, 10 contigous blocks are created. We can access first element from an array using code anArray[0], then second element using anArray[1] and next so on.

We can also declare and allocate memory of an array in one single statement. Please check below example :

				
// declaring and creating an array of integers in single statement
int[] arrayVar = new int[5];

			

2.) Creating and Initializing array using shortcut syntax

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

			

In above example, the length of the array is determined by the number of values provided between braces and separated by commas.

We can also declare,create and initialize array of arrays which is called
multidimensional array. We will look at
multidimensional array in next chapter.

Notes:-

  • Once the length of the array is defined, it cannot be changed later.
  • The size of an array is also known as the length of an array.
  • If the length of an array is n, the first element of the array will be anArray[0] and the last element will be anArray[n-1]
  • If we did not store any value to an array, the array will store default value of that datatype by itself.

Array Initialization

In above example we have done array declaration and creation, now we will learn how to initialize array.

We can initialize array using below two methods.

1.) Initializing array using assign method

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

// initializing array
anArray[0] = 10; // initialize first element
anArray[1] = 20; // initialize second element
anArray[2] = 30; // initialize third element
anArray[3] = 40; // initialize fourth element
anArray[4] = 50; // initialize fifth element
anArray[5] = 60; // initialize sixth element
anArray[6] = 70; // initialize seventh element
anArray[7] = 80; // initialize eigth element
anArray[8] = 90; // initialize ninth element
anArray[9] = 100; // initialize tenth element


			

Here,
anArray is an array. which is holding 10 values of int type. Please see below figure:

DataStructure Array Initialization
Fig. Array Initialization

In above figure, 10 contigous blocks are created and intialized with the values. We can access first element from an array using code anArray[0], then second element using anArray[1] and next so on.

2.) Initializing array using shortcut syntax

				
int[] anArray = { 
    10, 20, 30,
    40, 50, 60, 
    70, 80, 90, 100
};

			

In above example, array is initialized by providing number of values between braces and separated by commas.

3.) Initializing array while declaration

				
int[] anArray = new int[]{ 
    10, 20, 30,
    40, 50, 60, 
    70, 80, 90, 100
};

			

In above example, array is initialized while declration using new operator and by providing number of values between braces and separated by commas.

❮ Previous
Big O Complexity Chart
Next ❯
Types of Array

All Rights Reserved. ReadAndCare