In Java, an array is used to hold fixed number of similar type elements. The length of an array is fixed, which cannot be changed after it is created (to have variable length refer ArrayList). In this guide, we will see various examples of Array declaration and initialization in Java.
Declaring an Array
You can simply declare an array by specifying the data type of elements it will hold, followed by square brackets ([]
) then followed by array name. See the example below.
int[] myArray;
Alternatively, you can declare an array like this as well.
int myArray[];
Both of these array declarations are valid, but the former is frequently used.
Creating an Array
Once the array is declared, you need to create it using the new Keyword. You need to also specify the size of array, which means how many elements it can hold (also known as length of array).
// creates an array that can hold upto 10 integers
myArray = new int[10];
Declaration and creation in single line: You can do declaration and creation of array in one statement:
int[] myArray = new int[10];
Initializing an Array
Initializing means assigning the elements to the array. This can be done during declaration as well, see the following snippet:
int[] myArray = {1, 2, 3, 4, 5};
Let’s see this for other types of array:
double[] myDoubleArray = {1.1, 2.2, 3.3};
String[] myStringArray = {"Hello", "World"};
char[] myCharArray = {'a', 'b', 'c', 'd'};
Examples
1. Declaring, Creating, and Initializing an Integer Array:
int[] numbers = new int[5];
numbers[0] = 10;
numbers[1] = 20;
numbers[2] = 30;
numbers[3] = 40;
numbers[4] = 50;
2. Declaring and Creating an Array, then Initializing Using a Loop:
// Declare and create an array of 5 integers
int[] numbers = new int[5];
for (int i = 0; i < numbers.length; i++) {
// Initialize each element to i * 10
numbers[i] = i * 10;
}
3. Direct Initialization of an Array:
// Declaration, creation, and initialization in one line
int[] numbers = {10, 20, 30, 40, 50};
Multi-Dimensional Arrays
In the above example, we learned how to declare, create and initialize a 1D Array, let’s see how to do the same for 2D array.
Declaring, Creating, and Initializing a Two-Dimensional Array:
// Declare and create a 3x3 matrix
// 3 rows and 3 columns
int[][] matrix = new int[3][3];
// Initialize the first row, first index represents row
// and second index represents column
matrix[0][0] = 1; //first row, first column
matrix[0][1] = 2; //first row, second column
matrix[0][2] = 3; //first row, third column
// Initialize the second row
matrix[1][0] = 4;
matrix[1][1] = 5;
matrix[1][2] = 6;
// Initialize the third row
matrix[2][0] = 7;
matrix[2][1] = 8;
matrix[2][2] = 9;
Direct Initialization of a Two-Dimensional Array: Short and simple version of above statements:
int[][] matrix = {
{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
Array of Objects
Till now, we learned how array handles primitive types. However an array can hold objects as well. For example, an array of String
objects:
String[] names = new String[4];
names[0] = "Chaitanya";
names[1] = "Rahul";
names[2] = "Aditya";
names[2] = "Aarav";
// One line version of above statements
String[] names = {"Chaitanya", "Rahul", "Aditya", "Aarav"};
Summary
Let’s summarize everything briefly:
- Declaration:
data_type[] arrayName;
For example:int[] numbers;
- Creation:
arrayName = new data_type[size];
For example:numbers = new int[5]
- Initialization:
arrayName[index] = value;
ortype[] arrayName = {value1, value2, ...};
For example:numbers[0] = 1;
ornumbers = {1, 2, 3, 4, 5};
Leave a Reply