Arrays in Java Programming
Arrays in Java Programming
Arrays in Java Programming are objects that store elements of the same data type. The elements of an array are stored in a contiguous memory location.
-
- Only a fixed number of elements can be stored in an array.
- In Java, the array is index-based; the first element is indexed at index 0, the second element is indexed at 1, and so on such that the last element is stored at index n-1 where n is the number of elements of the array.
- The length of the array in Java can be obtained by using length members.
- Java arrays can store both primitive and object data types.
- Java array inherits the Object class and implements Serializable as well as Cloneable interfaces.
Array Creation in Java Programming
In Java, the syntax of array creation is as follow:
Syntax:
int[] arr=new arr[10];
The array created above is of type int and can store 10 elements at a contiguous memory location.
Array Initialization in Java Programming
Arrays in Java Programming can be initialized in two ways:
-
Initializing elements individually on separate lines
-
Initializing elements in a single line
Initializing elements individually on separate lines
After array creation, elements are assigned a value of a valid data type individually using indexing. Syntax of such initialization is given below:
Syntax:
int[] arr=new arr[5]; arr[0]=1; arr[1]=2; arr[2]=3; arr[3]=4; arr[4]=5;
Initializing elements in a single line
After array creation, the array is initialized by separating elements by a comma and then enclosed with a set of curly braces {}. Syntax of such initialization is given below:
Syntax:
int[] arr={1,2,3,4,5};
Access Elements of an Array in Java Programming
In Java, elements of an array are accessed using its index number.
Example to access Array Elements in Java:
//Java Program to access elements of an array public class ArrayAccess { public static void main(String[] args) { //Array declaration and initialization int[] arr={1,2,3,4,5}; //Accessing element at index 4 using the index System.out.println("Element at index 4 is: "+arr[4]); } }
Output:
Element at index 4 is: 5
Modify Elements of an Array in Java Programming
Elements of an array can be modified using its index number and assigning it a new value after accessing it.
Example to modify Array Elements in Java:
//Java program to modify elements of an array public class ArrayModify { public static void main(String[] args) { //Array declaration and initialization int[] arr={1,2,3,4,5}; //Accessing element at index 4 using the index System.out.println("Element at index 4 before modification is: "+arr[4]); //Accessing element at index 4 using the index and assigning it a new value arr[4]=10; System.out.println("Element at index 4 after modification is: "+arr[4]); } }
Output:
Element at index 4 before modification is: 5 Element at index 4 after modification is: 10
Number of Elements in an Array in Java Programming
A number of elements in an array can be obtained using the length member or property of the Array class.
Example to find number of Array Elements in Java:
//Java program to find number of elements in an array public class ArrayLength { public static void main(String[] args) { //Array declaration and initialization int[] arr={1,2,3,4,5}; //Length of the array System.out.println("Number of elemnets in arr[] is: "+arr.length); } }
Output:
Number of elemnets in arr[] is: 5
For Loop over an Array in Java Programming
Loop are used with arrays to efficiently access their elements and do necessary manipulations.
//Java Program to display array elements using for loop public class ArrayForLoop { public static void main(String[] args) { //Array declaration and initialization int[] arr={1,2,3,4,5}; //Displaying array elements using for loop for(int i=0;i<arr.length;i++){ System.out.println("arr["+i+"] is "+arr[i]); } } }
Output:
arr[0] is 1 arr[1] is 2 arr[2] is 3 arr[3] is 4 arr[4] is 5
For-each Loop over an Array in Java Programming
For-each loop is generally used to traverse the array and is element-based.
//Java Program to traverse an array using for-each loop public class ArrayForEach { public static void main(String[] args) { //Array declaration and initialization String[] sentence={"Welcome","to","StudyExperts","!"}; for(String word:sentence){ System.out.print(word+" "); } } }
Output:
Welcome to StudyExperts !
Multi-dimensional Array in Java Programming
The multi-dimensional array can be considered an array of arrays. An array of any dimensions can be created in Java and loops are used to work with them.
Syntax of Multi-dimensional Array Declaration:
//2-dimensional array int[][] arr1; //3-dimensional array int[][][] arr2;
Syntax of Multi-dimensional Array Declaration and Initialization:
//2-dimensional array with 2 rows and 3 columns for each row int[][] arr={{1,2,3},{4,5,6}};
Example of multi-dimensional array in Java:
//Java Program with 2-dimensional array public class ArrayTwoD { public static void main(String[] args) { //Array declaration and Initialization int[][] arr={{1,2,3},{4,5,6},{7,8,9}}; //Nested For Loop for(int i=0;i<arr.length;i++){ for(int j=0;j<arr[i].length;j++){ System.out.print(arr[i][j]+" "); } System.out.println(); } } }
Output:
1 2 3 4 5 6 7 8 9