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
In Java, we can declare an array with the following line of code:
// declares an array of dataType
dataType[] arrayVariableName;
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.
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.
// 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:
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];
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.
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.
// 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:
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.
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.
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.
All Rights Reserved. ReadAndCare