2. Contents
Introduction to Array
One Dimensional Array
ArrayIndexOutOfBoundsException
Multi-Dimensional Array
Passing Array to a method
Jagged Array
Class Name of Array
3. arrays
As we are study earlier, an array is a collection of similar type of elements which has contiguous memory
location.
Java Array is an object which contains elements of similar data-type and stored contiguously in memory.
Each items in an array is called element and each element is accessed by its numerical index. The index of
an array begins with zero(0).
4. arrays
Following are some important points about Java Arrays:
In java all arrays are dynamically allocated.
Since Java Array is an object, therefore we can find their length using the object property length.
Java array can also be used as static field, a local variable or a method parameter.
Java Array inherits the Object class and implements the Serializable as well as Cloneable interfaces. We
can store primitive values or objects in an array in Java.
Types of Array
One Dimensional Array
Multi Dimensional Array
5. One dimensional array
Syntax to Declare one dimensional Array:
<Data-Type> <Array-Name>[];
OR
<Data-Type>[] <Array-Name>;
Instantiation of Array:
ArrayRefVar = new <Data-Type>[Size];
Example –
int arr[]; // Declaration of array
arr = new int[10]; // Initialization of array
OR int arr[] = new int[10] // Declaration and initialization
OR
int arr[] = {10,20,30};
6. One dimensional array
//ArrayDemo1.java
class ArrayDemo1 {
public static void main(String args[]) {
int arr[];
arr = new int[5];
arr[0] = 10;
arr[1] = 11;
arr[2] = 12;
arr[3] = 13;
arr[4] = 14;
for(int i=0; i<arr.length; i++) {
System.out.println(arr[i]);
}
}
}
Output-
10
11
12
13
14
//ArrayDemo2.java
class ArrayDemo2 {
public static void main(String args[]) {
int arr[] = {10,20,30,40,50};
for(int i=0; i<arr.length; i++) {
System.out.println(arr[i]);
}
}
}
Output-
10
20
30
40
50
7. One dimensional array
//ArrayDemo3.java
import java.util.Scanner;
class ArrayDemo3 {
public static void main(String args[]) {
int n, sum = 0;
Scanner sc = new Scanner(System.in);
System.out.println("Enter the number of elements in array: ");
n = sc.nextInt();
int arr[] =new int[n];
System.out.println("Enter all the Elements: ");
for(int i=0; i<arr.length; i++) {
arr[i] = sc.nextInt();
sum = sum+arr[i];
}
System.out.println("Sum of all the Elements = "+sum);
}}
Output-
Enter the number of elements in array:
4
Enter all the Elements:
23
23
23
23
Sum of all the Elements = 92
Input through Keyboard in Java Array.
8. One dimensional array
Passing an array as a parameter to a
Method in java
//ArrayDemo4.java
class ArrayDemo4 {
void display(int ar[]) {
for(int i=0; i<ar.length; i++) {
System.out.println(ar[i]);
}
}
public static void main(String args[]) {
ArrayDemo ad = new ArrayDemo();
int a[] = {10,20,30};
ad.display(a);
}
}
Output-
10
20
30
9. One dimensional array
ArrayIndexOutOfBoundsException
If length of the array in negative, equal to
the array size or greater than the array size
while traversing the array.
//ArrayDemo5.java
class ArrayDemo4 {
void display(int ar[]) {
for(int i=0; i<=ar.length; i++) {
System.out.println(ar[i]);
}
}
public static void main(String args[]) {
ArrayDemo ad = new ArrayDemo();
int a[] = {10,20,30};
ad.display(a);
}
}
Output-
10
20
30
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 3
at ArrayDemo.display(ArrayDemo.java:4)
at ArrayDemo.main(ArrayDemo.java:10)
10. Multi - dimensional array
In Multi – Dimensional Array, Data are stored in the form of table i.e., row and column or we can say that
in the form of matrix.
Syntax of Declaration
<DataType>[][] arr (or) <DataType> arr[][]; (or) <DataType> []arr[];
Instantiate Multidimensional Array in Java
int [][]arr = new int[3][3]; // 3 row and 3 column
First index represent the Row and Second represent the column.
arr[0][0] arr[0][1] arr[0][2]
arr[1][0] arr[1][1] arr[1][2]
arr[2][0] arr[2][1] arr[2][2]
13. Jagged array
Jagged Array is array of arrays such that member
arrays can be of different sizes, i.e., we can create a
2-D arrays but with variable number of columns in
each row. These type of arrays are also known as
Jagged arrays.
//TwoDArray3.java
class TwoDArray3 {
public static void main(String[] args) {
int arr[][] = new int[2][];
arr[0] = new int[3];
arr[1] = new int[2];
int count = 0;
for (int i=0; i<arr.length; i++)
for(int j=0; j<arr[i].length; j++)
arr[i][j] = count++;
System.out.println("Contents of 2D Jagged Array");
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-
Contents of 2D Jagged Array
0 1 2
3 4
14. Class name of java array
In Java, an array is an object. For array object, a
proxy class is created whose name can be
obtained by getClass().getName() method on the
object.
//TwoDArray4.java
class TwoDArray4 {
public static void main(String[] args) {
int arr[]={10,20,30};
Class c=arr.getClass();
String name=c.getName();
System.out.println(name);
}
}
Output -
[I