In general, “array” means an arrangement of similar things in a particular way. On the other hand, we can say the arrays in Java is grouping the things having same features.
In Java programming, the array is used to store multiple values in a single variable. We can also say an array stores multiple variables of same data types in a single unit.

Related Links:
- Basic Java – 1 || Understand Java before start learning JAVA.
- Basic Java – 2 || Variables and Data Types used in JAVA.
- Basic Java – 3 || Understanding Class, Objects, Methods in Java.
- Basic Java – 4 || More on methods(Return Type and Parameters)
- Basic Java – 5 || Methods- Call by Value and Call by Reference in Java.
- Basic Java – 6 || Understanding of Constructor and Destructor in JAVA.
- Basic Java – 7 || Static Variables and Methods.
- Basic Java – 8 || Lets learn about Arrays in Java.
- Basic Java – 9 || Performing multiple operations using Java Operators.
- Basic Java – 10 || Conditions (If and Switch) in JAVA.
- Basic Java – 11 || for and for-each in Java. (Loops Part-1)
- Basic Java – 12 || Alternate looping concepts while and do-while. (Loops Part-2)
- Basic Java – 13 || Decimal values v/s Octal base(8) values in JAVA.
- Basic Java – 14 || Learn about String literals in Java.
- Basic Java – 15 || Runtime User Input using Scanner Class (Part-1).
- Basic Java – 16 || Runtime User Input using BufferedReader Class (Part-2).
- Basic Java – 17 || Runtime User Input using Console Class (Part-3).
- Basic Java – 18 || Difference between break and continue keywords.
- Basic Java – 19 || Sending Email using Java (Part-1).
- Basic Java – 20 || Sending Email with attachment using Java (Part-2).
- Basic Java – 21 || Stack memory and Heap memory in Java.
- Basic Java – 22 || Let’s learn more about String.
- Basic Java – 23 || String, StringBuffer & StringBuilder in Java.
- Basic Java – 24 || Exception Handling using Try Catch.
- QnA || How to use Constructors in Abstract class?
- QnA | Difference between Integer and int keywords.
- QnA | Can main method be overloaded?
- QnA | How do I reverse a String/Sentence in Java?
- QnA | Perform Multiplication and Division without * or / or % operators.
- QnA | How do I get the default value of data type?
- QnA | How to split String if it contains period symbol (.) in between?
Declare and initialize array
<DataType>[] <VariableName> =new <DataType>[ArrayLength]
<variableName>[index]=<variableValue>
OR
<DataType>[] <VariableName>={<value1, value2, value3….>}
Example:
int[] rollNumber=new int[5];
int[1]=50;
OR
int[ ] rollNumber={3,57,6,89,60}
Access values from an array:
int x=rollNumber[2];
‘x will store value 6 from the array.’
Process and Structure of Array
-
- DataType: Define data type of array variable.
- Square brackets[ ], denotes that currently declared variable is of array type.
- VariableName is the name of an array.
- new, the keyword used to initiate object of the expected data type.
- ArrayLength defines the total number of values, which can be stored in this array variable.
- [index], used to access and store values on a specific position in the array.
Limitation of Array
- Array allocated memory as defined in ‘ArrayLength’ field. So, it can store only given number of values. Length of array remains fixed.
- We can’t create a dynamic array, so the length of the array cannot be increased or decreased at run time.
- The memory remains occupied even after removing values from an array.
- We can access values in a specific position using index number. The index will always start with 0.
Types of Array
- Single Dimensional Array: Already described above.
- Two Dimensional Array: Process to initialize and access the two-dimensional array would be same as the single dimensional array.

A two-dimensional array stores data in a table using row and column format. As in the above image, it is clear that we have declared a two-dimensional array having 3 rows and 2 columns.
int[ ][ ] arrayVar = new int[row][col];
In a two-dimensional array, the first index depicts row and second index shows column count. Note that both the index would always start from 0.
Example:
Int[ ][ ] rollNumber=new int[5][2];
int[1][0]=50;
OR
int[ ][ ] rollNumber = {
{10,9},
{8,7},
{6,5},
{4,3},
{2,1} };
Access values from an array:
int x=rollNumber[2][1];
‘x will store value 5 from the array.’
Code Example Single Dimensional Array
public class SingleDim { public static void main(String[] args) { // Declare array variable with length 5. int[] rollNumber=new int[5]; // Initialize and assign values to array variable for all positions using index. rollNumber[0]=10; rollNumber[1]=9; rollNumber[2]=8; rollNumber[3]=7; rollNumber[4]=6; // Extracting and storing values in other variables int temp1=rollNumber[2]; int temp2=rollNumber[4]; // Print values from other variables System.out.println("temp1: "+temp1); System.out.println("temp2: "+temp2); // We can also access the array values directly System.out.println("\nDirect access of array values:"); System.out.println(rollNumber[0]); System.out.println(rollNumber[1]); System.out.println("\nAlternate proecss to declare and initialize a array variable :"); int[] tempArray={5,4,3}; System.out.println(tempArray[0]); System.out.println(tempArray[1]); System.out.println(tempArray[2]); // If we try to access an undefined index value, it will throw 'ArrayIndexOutOfBoundExceptin' System.out.println(tempArray[10]); } }
Code Example of Two-Dimensional Array
public class TwoDim { public static void main(String[] args) { // Declare Two dimensional array variable with 3 row and 2 Columns int[][] rollNumber=new int[3][2]; // Initialize and assign values to array variable for all positions using index. rollNumber[0][0]=10; rollNumber[0][1]=9; rollNumber[1][0]=8; rollNumber[1][1]=7; rollNumber[2][0]=6; rollNumber[2][1]=5; // Extracting and storing values in other variables int temp1=rollNumber[1][1]; int temp2=rollNumber[2][0]; // Print values from other variables System.out.println("temp1: "+temp1); System.out.println("temp2: "+temp2); // We can also access the array values directly System.out.println("\nDirect access of array values:"); System.out.println(rollNumber[0][0]); System.out.println(rollNumber[1][0]); System.out.println("\nAlternate proecss to declare and initialize a array variable having 2 rows and 2 columns :"); int[][] tempArray={ {5,4}, {3,2} }; System.out.println(tempArray[0][0]); System.out.println(tempArray[0][1]); System.out.println(tempArray[1][0]); System.out.println(tempArray[1][1]); } }