Java Programming
Manish Kumar
(manish87it@gmail.com)
Lecture- 7
Java arrays
Contents
 Introduction to Array
 One Dimensional Array
 ArrayIndexOutOfBoundsException
 Multi-Dimensional Array
 Passing Array to a method
 Jagged Array
 Class Name of Array
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).
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
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};
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
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.
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
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)
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]
Multi - dimensional array
//TwoDArray1.java
class TwoDArray1 {
public static void main(String args[]){
int ar[][] = {{10,20,30},{40,50,60},{70,80,90}};
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
System.out.print(ar[i][j]+" ");
}
System.out.println();
}
}
}
Output-
10 20 30
40 50 60
70 80 90
Multi - dimensional array
//TwoDArray2.java
import java.util.Scanner;
class TwoDArray2 {
public static void main(String args[]){
int ar[][] = new int[3][3];
Scanner sc = new Scanner(System.in);
System.out.println("Enter Array's Elements :");
for(int i=0; i<3; i++) {
for(int j=0; j<3; j++) {
ar[i][j] = sc.nextInt();
System.out.println("ar["+i+"]["+j+"] = "+" "+ar[i][j]);
}
System.out.println();
}}
}
Enter Array's Elements :
12
ar[0][0] = 12
13
ar[0][1] = 13
14
ar[0][2] = 14
16
ar[1][0] = 16
18
ar[1][1] = 18
19
ar[1][2] = 19
20
ar[2][0] = 20
21
ar[2][1] = 21
22
ar[2][2] = 22
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
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
Lecture   7 arrays

Lecture 7 arrays

  • 1.
  • 2.
    Contents  Introduction toArray  One Dimensional Array  ArrayIndexOutOfBoundsException  Multi-Dimensional Array  Passing Array to a method  Jagged Array  Class Name of Array
  • 3.
    arrays  As weare 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 someimportant 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 Syntaxto 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 classArrayDemo1 { 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 importjava.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 Passingan 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 Iflength 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 - dimensionalarray 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]
  • 11.
    Multi - dimensionalarray //TwoDArray1.java class TwoDArray1 { public static void main(String args[]){ int ar[][] = {{10,20,30},{40,50,60},{70,80,90}}; for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { System.out.print(ar[i][j]+" "); } System.out.println(); } } } Output- 10 20 30 40 50 60 70 80 90
  • 12.
    Multi - dimensionalarray //TwoDArray2.java import java.util.Scanner; class TwoDArray2 { public static void main(String args[]){ int ar[][] = new int[3][3]; Scanner sc = new Scanner(System.in); System.out.println("Enter Array's Elements :"); for(int i=0; i<3; i++) { for(int j=0; j<3; j++) { ar[i][j] = sc.nextInt(); System.out.println("ar["+i+"]["+j+"] = "+" "+ar[i][j]); } System.out.println(); }} } Enter Array's Elements : 12 ar[0][0] = 12 13 ar[0][1] = 13 14 ar[0][2] = 14 16 ar[1][0] = 16 18 ar[1][1] = 18 19 ar[1][2] = 19 20 ar[2][0] = 20 21 ar[2][1] = 21 22 ar[2][2] = 22
  • 13.
    Jagged array Jagged Arrayis 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 ofjava 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