Array In JAVA With Examples

Array can be defined as a contiguous memory locations used to store the homogeneous data types. In simple words, it is a variable that can store multiple values of single data type.

  • The integer array can be declared as int[] intArray; (recommended by JAVA) or int intArray[]; (not recommended by JAVA). Here intArray is the name of Array variable.
  • The length/size of array is fixed and defined when array is created. For example, intArray = new int[5]; fix the length of Array with variable name intArray up to 5 values which means it can store 5 values of integer data type.
    int[] intArray; // Array Declared
    intArray = new int[5]; // Array length is established gets memory for 5 integers which is fix
    intArray[0] = 10; // Value stored at index 0 position
    intArray[1] = 20; // Value stored at index 1 position
    //So on... we can store upto 5 values and index 4 position
  • Alternatively we can also declare array using shorter syntax: int[] intArray = {10,20,30,40,50}; . In this case the total number of values is the size of array and also values are directly stored in it starting with index 0.

Array Explanation: Index And Element Value:

int[] intArray = {10,20,30,40,50};

Index Value
Element Value
0 10
1 20
2 30
3 40
4 50

Let’s now understand array concept from the above table which shows an array with 5 integer values stored in it. Two things are very clear at seeing on first sight from the table i.e. array is having an index value which starts from 0 and other is element value. Both index and element value have their own meaning. Index value is simple to take the track of your array. For example, we can track value 20 by using intArray[1];. Element value is the value stored in array.

Important Note 1: Index value starts with zero and move on in increasing order. It has upper bound and lower bound. Upper bound is always one less than the size of given array.

Important Note 2: The first element of array is accessed at index 0 i.e. intArray[0] in the above table. And the last element can be accessed by one less index value of total i.e. intArray[4] in the above table.


Array Declaration In JAVA

To assign some value in the memory there are two thing mandatory in program. First is its declaration and then its initialization. So same principle is applied on Array also. To declare it we can simply write as

int[] onedimensionalarray= new int[5];

//OR

int onedimendionalarray[]=new int[5];

Both the ways are right, but first one is preferred as recommended by JAVA. This is to declare an one dimensional array of integer type.

Now the second thing is to initialize that particular variable. Here it is assigned the values using index of that particular array. E.g.

onedimensionalarray[0]=10; // Particular value is initialized

Here first index of array is initialized with value 10. In similar way you can simply assign the value to the different index values.

Important Note: The important thing about the array is to remember its size is fixed after its creation i.e. when memory has been allocated.

Another way to declare and initialize the given array:

We can also use shorter index to declare and directly initialize values:

int[] onedimensionalarray= {10,30,20,50,15};

Similarly we can declare other primitive data types and String array in JAVA.

byte[] anArrayOfBytes; // Byte Array Declaration
short[] anArrayOfShorts; // Short Array Declaration
long[] anArrayOfLongs; // Long array declaration
float[] anArrayOfFloats; //Float array declaration
double[] anArrayOfDoubles; //Double array declaration
boolean[] anArrayOfBooleans; // Boolean Arrray Declaration
char[] anArrayOfChars; //Character array declaration
String[] anArrayOfStrings; //String Array Declaration

Array Examples, Program And Code:

Now lets build some Array programs to understand the Array concept more deeply.

Example 1: This is a very simple program in which integer Array is declared, initialized and finally all its value is printed using For loop.

public class ArraysInJAVA {

	public static void main(String[] args) {
		int[] onedimensionalarray= new int[5]; // Array declared with size
		onedimensionalarray[0]=10; // Value initialized
		onedimensionalarray[1]=20;
		onedimensionalarray[2]=30;
		onedimensionalarray[3]=40;
		onedimensionalarray[4]=50;

// Values are printed in output
		
		for(int i=0; i<5;i++){
			System.out.println("the value in onedimensioanlarray are " +onedimensionalarray[i]);
		}
	}

}

Output:

the value in onedimensioanlarray are 10
the value in onedimensioanlarray are 20
the value in onedimensioanlarray are 30
the value in onedimensioanlarray are 40
the value in onedimensioanlarray are 50

Example 2: The second example of Array is of String because lots of JAVA beginners are confused with it though the process is simple and same.

If you consider the case for array of string, in this there is also same principle apply as array of integers. You can access any string from the array using the particular index value of for any string to which you want to access.

Like if we take an array of string of size 4. First of all we must know how to declare array of string. This can be written as:

String[] onedimensionalarray= new String[4];

And after doing this the next thing is to assign the different string elements to this array which we can access later on when we have its requirement. To access the elements from this array simply we can use For loop which will follow 4 iterations and in these iterations array elements can be printed very easily.

public class ArrayStringJava {

	public static void main(String[] args) {
		String[] onedimensionalarray=new String[4]; //String Array Declared
		onedimensionalarray[0]="Java "; // Value intialized
		onedimensionalarray[1]="Is ";
		onedimensionalarray[2]="High Level ";
		onedimensionalarray[3]="Language";

//String value printed
		
		for(int i=0; i<onedimensionalarray.length;i++){
			System.out.print(onedimensionalarray[i]);
		}

	}

}

Output:

Java Is High Level Language

Multidimensional Array In JAVA:

Array is continuous memory locations used to store homogeneous data means a data of similar type and Multi-Dimensional Array is used to store the values in the rows as well as in columns.

Important Note: Java does not support multidimensional array. Multidimensional array in java is basically “array of arrays”.

Below is multi-dimensional array declaration syntax:

int[][] twoDimensionalArray= new int[2][3];

Multi-dimensional Array initialization:

int[][] twoDArray=new int[2][2];
        
    /*value assigned are 1,2,4,5 at [0][0],[0][1],[1][0],[1][1] index values respectively*/
        twoDArray[0][0]=1; 
        twoDArray[0][1]=2;
        twoDArray[1][0]=4;
        twoDArray[1][1]=5;

For complete details read Multidimensional tutorial with example


Array Important Points, Summary And Quick Revision:

  • Array is data structure which is static in nature. This is different from the type of array used in c/c++/Fortran where its size put no limit on entering the element. In case of java if you put the element beyond its limit then it shows compile time exception of out of index array exception.
  • Array is used to store multiple values of same data type in single variable
  • The size of array is fixed and defined when it is created
  • Array index value start with 0

Leave a Reply

Your email address will not be published. Required fields are marked *