1. Java array is an object which contains elements of a similar data type.
Additionally, The elements of an array are stored in a contiguous memory
location. It is a data structure where we store similar elements. We can store only
a fixed set of elements in a Java array.
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.
Unlike C/C++, we can get the length of the array using the length member. In
C/C++, we need to use the sizeof operator.
2. There are two types of array.
Single Dimensional Array
Multidimensional Array
3. Syntax to Declare an Array in Java
dataType[] arr; (or)
dataType []arr; (or)
dataType arr[];
Instantiation of an Array in Java
arrayRefVar=new datatype[size];
4. int marks[ ] = { 90, 97, 95, 99, 100 }; // declare marks[ ] and initialize with five values.
int marks[ ]; // declare marks arrays.
marks = new int[5]; // allocating memory for storing 5 integer elements into an array.
int marks[ ]=new int[5]; // declare and allocate memory for 5 ints
6. package arraysProgram;
public class OneDArr {
public static void main(String[] args)
{
// Declare and initialize an array of five integer values.
int arr[] = {2, 4, 6, 8, 10};
// Display all five elements on the console.
for (int i = 0; i < arr.length; i++)
System.out.println(arr[i] + " ");
}
}
7. package arraysProgram;
public class OneDArr {
public static void main(String[] args)
{
// Declare and initialize an array of five integer values.
int[ ] num = {2, 4, 6, 8, 10, 12, 14};
// Display the length of array.
System.out.println("Length of array: " +num.length);
}
}
8. A two dimensional array in java represents many rows and columns of data.
Data in matrix or table form can be represented by using two dimensional array.
9. The syntax to create a two dimensional array and directly store elements at the time of
its declaration
int marks[ ] [ ] = {{50, 60, 78, 87, 95},
{98, 87, 75, 76, 99},
{98, 99, 97, 95, 100} };
10. In a two-dimensional array, an element is accessed through a row and column index.
2. The second way for creating a two dimensional array is that first declare an array
and then allocate memory for it using the new operator.
int[ ][ ] marks; // declaring a marks array. marks = new int[3][5]; // allocating
memory for storing 15 elements.
int[ ][ ] marks = new int[3][5];
JVM allocates memory for storing 15 integer values into the array. But so far, we have
not stored 15 values into an array.
11. package arraysProgram;
public class TwoDArrayExample {
public static void main(String[] args)
{
// Create a 2D array of int type.
int[ ][ ] x = {{1, 2, 3},
{4, 5, 6},
{7, 8, 9}
};
// Read and display array elements in a matrix form. System.out.println("Displaying
elements of 2D array in a matrix form:");
// Applying nested for loop.
for(int i = 0; i < 3; i++) // Outer for loop for Rows
{
for(int j = 0; j < 3; j++) // Columns in each row.
{
System.out.print(x[i][j]+ "t");
}
System.out.println(); // next line.
}
}
}