An Array can contain fixed number of elements of similar type.
Once the array is created, its size is fixed.
Size of an array can be specified as int only as the arrays are in-indexed.
Indexing in arrays is 0-based. So, the first element of an array numbers can be accessed as numbers[0], second element as numbers[1] and so on.. the last element can be accessed as numbers[numbers.length-1].
Creating and Initializing Primitive, Single dimensional arrays
We can declare an array variable and specify the size of it and then assign individual elements.
1 2 3 4 5 6 | int[] numbers = new int[2] ; numbers[0] = 10; numbers[1] = 20; |
Arrays use 0-based index. So, following code will throw an ArrayIndexOutOfBoundsException when executing :
1 2 3 4 5 | int[] numbers = new int[2] ; numbers[2] = 30;//java.lang.ArrayIndexOutOfBoundsException: 2 |
We can combine the steps to declare and initialize arrays as shown below :
1 2 3 4 | int[] numbers = new int[]{10,20}; |
Here is a shortcut for the same :
1 2 3 4 | int[] numbers = {10,20}; |
Reference Arrays
Similar to primitive arrays, we can declare and initialize reference arrays.
In the following example, we create an array of Employee objects.
Here is the Employee class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 | package com.topjavatutorial; public class Employee { private String empName; private long empId; public Employee(long empId, String empName){ this.empId = empId; this.empName = empName; } public String getEmpName() { return empName; } public long getEmpId() { return empId; } public String toString(){ return empName; } } |
Now lets see the code that create and iterates through the reference array :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.topjavatutorial; public class Hello { public static void main(String[] args) { Employee[] employees = new Employee[2]; employees[0] = new Employee(1,"John Doe"); employees[1] = new Employee(2,"Dave Smith"); for (int i = 0; i < employees.length; i++) { System.out.println(employees[i]); } } } |
Output:
John Doe
Dave Smith
Creating and Initializing Multidimensional Arrays
The following code declares and initializes a 2-dimensional array named “matrix” :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | int[][] matrix = new int[2][2]; matrix[0][0] = 10; matrix[0][1] = 20; matrix[1][0] = 30; matrix[1][1] = 40; for (int i=0; i<matrix.length; i++) { for (int j=0; j<matrix[i].length; j++) System.out.print(matrix[i][j] + " "); System.out.println(); } |
Output :
10 20
30 40
We can use the shortcut syntax and also declare and initialize in following manner :
1 2 3 4 | int[][] matrix = {{10,20},{30,40}}; |
Creating and Initializing Jagged Array in Java
Jagged array is array of arrays such that member arrays can be of different sizes.
The following program creates a 2D Jagged Array with variable number of columns in each row :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | package com.topjavatutorial; public class Hello { public static void main(String[] args) { // Declare the jagged array int matrix[][] = new int[2][]; matrix[0] = new int[3]; matrix[1] = new int[4]; // Initialize the jagged array for (int i = 0; i < matrix.length; i++) for (int j = 0; j < matrix[i].length; j++) matrix[i][j] = i + j; // print the jagged array for (int i = 0; i < matrix.length; i++) { for (int j = 0; j < matrix[i].length; j++) System.out.print(matrix[i][j] + " "); System.out.println(); } } } |
Output:
0 1 2
1 2 3 4
© 2016, www.topjavatutorial.com. All rights reserved. On republishing this post, you must provide link to original post