SlideShare a Scribd company logo
6
Most read
13
Most read
14
Most read
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

More Related Content

PPTX
Thread priorities
PPT
Jdbc ppt
PPT
Packages in java
PPT
Java interfaces
PPTX
Interface in java
PPTX
I/O Streams
PPTX
Java string handling
PPTX
Chapter 3 servlet & jsp
Thread priorities
Jdbc ppt
Packages in java
Java interfaces
Interface in java
I/O Streams
Java string handling
Chapter 3 servlet & jsp

What's hot (20)

PPTX
java interface and packages
PPS
Jdbc architecture and driver types ppt
PDF
Java - File Input Output Concepts
PPTX
Packages in java
PDF
Java threads
PPS
Interface
PPTX
Types of Drivers in JDBC
PPTX
Classes, objects in JAVA
PPTX
Java Stack Data Structure.pptx
PDF
Collections in Java Notes
PDF
Java Thread Synchronization
PPTX
Strings in Java
PPTX
JAVA-PPT'S.pptx
PDF
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
PPT
Array in Java
PPTX
Super keyword in java
PPTX
servlet in java
PPTX
Inheritance in java
java interface and packages
Jdbc architecture and driver types ppt
Java - File Input Output Concepts
Packages in java
Java threads
Interface
Types of Drivers in JDBC
Classes, objects in JAVA
Java Stack Data Structure.pptx
Collections in Java Notes
Java Thread Synchronization
Strings in Java
JAVA-PPT'S.pptx
Chapter 02: Classes Objects and Methods Java by Tushar B Kute
Array in Java
Super keyword in java
servlet in java
Inheritance in java
Ad

Similar to Lecture 7 arrays (20)

PPTX
Java Programming
PPTX
6_Array.pptx
PPTX
Arrays in Java with example and types of array.pptx
PDF
PPTX
CH1 ARRAY (1).pptx
DOCX
Class notes(week 4) on arrays and strings
PDF
Class notes(week 4) on arrays and strings
PPTX
SessionPlans_f3efa1.6 Array in java.pptx
PDF
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
PPTX
arrays in c# including Classes handling arrays
PPTX
Unit-2.Arrays and Strings.pptx.................
PPTX
PPTX
Basic java, java collection Framework and Date Time API
DOCX
Write an application that stores 12 integers in an array. Display the.docx
DOCX
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
DOCX
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
PPTX
Java arrays
PPT
array: An object that stores many values of the same type.
PPT
Arrays (Lists) in Python................
Java Programming
6_Array.pptx
Arrays in Java with example and types of array.pptx
CH1 ARRAY (1).pptx
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
SessionPlans_f3efa1.6 Array in java.pptx
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
arrays in c# including Classes handling arrays
Unit-2.Arrays and Strings.pptx.................
Basic java, java collection Framework and Date Time API
Write an application that stores 12 integers in an array. Display the.docx
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java arrays
array: An object that stores many values of the same type.
Arrays (Lists) in Python................
Ad

More from manish kumar (8)

PPTX
Lecture 9 access modifiers and packages
PPTX
Lecture 8 abstract class and interface
PPTX
Lecture 6 inheritance
PPTX
Lecture - 5 Control Statement
PPTX
Lecture 4_Java Method-constructor_imp_keywords
PPTX
Lecture - 3 Variables-data type_operators_oops concept
PPTX
Lecture - 2 Environment setup & JDK, JRE, JVM
PPTX
Lecture - 1 introduction to java
Lecture 9 access modifiers and packages
Lecture 8 abstract class and interface
Lecture 6 inheritance
Lecture - 5 Control Statement
Lecture 4_Java Method-constructor_imp_keywords
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 1 introduction to java

Recently uploaded (20)

PDF
Classroom Observation Tools for Teachers
PDF
01-Introduction-to-Information-Management.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Complications of Minimal Access Surgery at WLH
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Institutional Correction lecture only . . .
PPTX
Lesson notes of climatology university.
PDF
VCE English Exam - Section C Student Revision Booklet
PDF
Basic Mud Logging Guide for educational purpose
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PPTX
Cell Types and Its function , kingdom of life
PPTX
GDM (1) (1).pptx small presentation for students
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
Classroom Observation Tools for Teachers
01-Introduction-to-Information-Management.pdf
O7-L3 Supply Chain Operations - ICLT Program
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Anesthesia in Laparoscopic Surgery in India
Complications of Minimal Access Surgery at WLH
Microbial disease of the cardiovascular and lymphatic systems
Institutional Correction lecture only . . .
Lesson notes of climatology university.
VCE English Exam - Section C Student Revision Booklet
Basic Mud Logging Guide for educational purpose
Module 4: Burden of Disease Tutorial Slides S2 2025
Supply Chain Operations Speaking Notes -ICLT Program
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
2.FourierTransform-ShortQuestionswithAnswers.pdf
Cell Types and Its function , kingdom of life
GDM (1) (1).pptx small presentation for students
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape

Lecture 7 arrays

  • 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]
  • 11. 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
  • 12. 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
  • 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