SlideShare a Scribd company logo
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
Ad

More Related Content

What's hot (20)

input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
sharma230399
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
Abhilash Nair
 
Applets
AppletsApplets
Applets
Prabhakaran V M
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Hamid Ghorbani
 
Java keywords
Java keywordsJava keywords
Java keywords
Ravi_Kant_Sahu
 
Access specifiers(modifiers) in java
Access specifiers(modifiers) in javaAccess specifiers(modifiers) in java
Access specifiers(modifiers) in java
HrithikShinde
 
Super keyword in java
Super keyword in javaSuper keyword in java
Super keyword in java
Hitesh Kumar
 
Inheritance ppt
Inheritance pptInheritance ppt
Inheritance ppt
Nivegeetha
 
File handling
File handlingFile handling
File handling
priya_trehan
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
INHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptxINHERITANCE IN JAVA.pptx
INHERITANCE IN JAVA.pptx
NITHISG1
 
Method Overloading in Java
Method Overloading in JavaMethod Overloading in Java
Method Overloading in Java
Sonya Akter Rupa
 
Applet and graphics programming
Applet and graphics programmingApplet and graphics programming
Applet and graphics programming
srijavel
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
shanmuga rajan
 
Packages in java
Packages in javaPackages in java
Packages in java
Kavitha713564
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Abhilash Nair
 
Type casting in java
Type casting in javaType casting in java
Type casting in java
Farooq Baloch
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
java interface and packages
java interface and packagesjava interface and packages
java interface and packages
VINOTH R
 

Similar to Lecture 7 arrays (20)

Java Programming
Java ProgrammingJava Programming
Java Programming
Nanthini Kempaiyan
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
shafat6712
 
Arrays in Java with example and types of array.pptx
Arrays in Java with example and types of array.pptxArrays in Java with example and types of array.pptx
Arrays in Java with example and types of array.pptx
ashwinibhosale27
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
Debasish Pratihari
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
AnkitaVerma776806
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
SessionPlans_f3efa1.6 Array in java.pptx
SessionPlans_f3efa1.6 Array in java.pptxSessionPlans_f3efa1.6 Array in java.pptx
SessionPlans_f3efa1.6 Array in java.pptx
businessmarketing100
 
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdfJava R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
kamalabhushanamnokki
 
arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arrays
JayanthiM19
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
suchitrapoojari984
 
Arrays
ArraysArrays
Arrays
mohamedjahidameers
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time API
jagriti srivastava
 
Write an application that stores 12 integers in an array. Display the.docx
 Write an application that stores 12 integers in an array. Display the.docx Write an application that stores 12 integers in an array. Display the.docx
Write an application that stores 12 integers in an array. Display the.docx
ajoy21
 
Array
ArrayArray
Array
Kongu Engineering College, Perundurai, Erode
 
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docxArraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Sunil Kumar Gunasekaran
 
Java arrays
Java    arraysJava    arrays
Java arrays
Mohammed Sikander
 
array: An object that stores many values of the same type.
array: An object that stores many values of the same type.array: An object that stores many values of the same type.
array: An object that stores many values of the same type.
KurniawanZaini1
 
Arrays (Lists) in Python................
Arrays (Lists) in Python................Arrays (Lists) in Python................
Arrays (Lists) in Python................
saulHS1
 
Arrays in Java with example and types of array.pptx
Arrays in Java with example and types of array.pptxArrays in Java with example and types of array.pptx
Arrays in Java with example and types of array.pptx
ashwinibhosale27
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and stringsClass notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Kuntal Bhowmick
 
SessionPlans_f3efa1.6 Array in java.pptx
SessionPlans_f3efa1.6 Array in java.pptxSessionPlans_f3efa1.6 Array in java.pptx
SessionPlans_f3efa1.6 Array in java.pptx
businessmarketing100
 
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdfJava R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
Java R20 - UNIT-3.pdf Java R20 - UNIT-3.pdf
kamalabhushanamnokki
 
arrays in c# including Classes handling arrays
arrays in c#  including Classes handling arraysarrays in c#  including Classes handling arrays
arrays in c# including Classes handling arrays
JayanthiM19
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
suchitrapoojari984
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time API
jagriti srivastava
 
Write an application that stores 12 integers in an array. Display the.docx
 Write an application that stores 12 integers in an array. Display the.docx Write an application that stores 12 integers in an array. Display the.docx
Write an application that stores 12 integers in an array. Display the.docx
ajoy21
 
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docxArraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
Arraysnklkjjkknlnlknnjlnjljljkjnjkjn.docx
pranauvsps
 
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Java programs - bubble sort, iterator, linked list, hash set, reverse string,...
Sunil Kumar Gunasekaran
 
array: An object that stores many values of the same type.
array: An object that stores many values of the same type.array: An object that stores many values of the same type.
array: An object that stores many values of the same type.
KurniawanZaini1
 
Arrays (Lists) in Python................
Arrays (Lists) in Python................Arrays (Lists) in Python................
Arrays (Lists) in Python................
saulHS1
 
Ad

More from manish kumar (8)

Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
manish kumar
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
manish kumar
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
manish kumar
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statement
manish kumar
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
manish kumar
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
manish kumar
 
Lecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVMLecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVM
manish kumar
 
Lecture - 1 introduction to java
Lecture - 1 introduction to javaLecture - 1 introduction to java
Lecture - 1 introduction to java
manish kumar
 
Lecture 9 access modifiers and packages
Lecture   9 access modifiers and packagesLecture   9 access modifiers and packages
Lecture 9 access modifiers and packages
manish kumar
 
Lecture 8 abstract class and interface
Lecture   8 abstract class and interfaceLecture   8 abstract class and interface
Lecture 8 abstract class and interface
manish kumar
 
Lecture 6 inheritance
Lecture   6 inheritanceLecture   6 inheritance
Lecture 6 inheritance
manish kumar
 
Lecture - 5 Control Statement
Lecture - 5 Control StatementLecture - 5 Control Statement
Lecture - 5 Control Statement
manish kumar
 
Lecture 4_Java Method-constructor_imp_keywords
Lecture   4_Java Method-constructor_imp_keywordsLecture   4_Java Method-constructor_imp_keywords
Lecture 4_Java Method-constructor_imp_keywords
manish kumar
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
manish kumar
 
Lecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVMLecture - 2 Environment setup & JDK, JRE, JVM
Lecture - 2 Environment setup & JDK, JRE, JVM
manish kumar
 
Lecture - 1 introduction to java
Lecture - 1 introduction to javaLecture - 1 introduction to java
Lecture - 1 introduction to java
manish kumar
 
Ad

Recently uploaded (20)

Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
The History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptxThe History of Kashmir Karkota Dynasty NEP.pptx
The History of Kashmir Karkota Dynasty NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 
Ajanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of HistoryAjanta Paintings: Study as a Source of History
Ajanta Paintings: Study as a Source of History
Virag Sontakke
 
Chemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptxChemotherapy of Malignancy -Anticancer.pptx
Chemotherapy of Malignancy -Anticancer.pptx
Mayuri Chavan
 
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living WorkshopLDMMIA Reiki Yoga S5 Daily Living Workshop
LDMMIA Reiki Yoga S5 Daily Living Workshop
LDM Mia eStudios
 
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales moduleHow To Maximize Sales Performance using Odoo 18 Diverse views in sales module
How To Maximize Sales Performance using Odoo 18 Diverse views in sales module
Celine George
 
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon DolabaniHistory Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
History Of The Monastery Of Mor Gabriel Philoxenos Yuhanon Dolabani
fruinkamel7m
 
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptxTERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
TERMINOLOGIES,GRIEF PROCESS AND LOSS AMD ITS TYPES .pptx
PoojaSen20
 
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
BÀI TẬP BỔ TRỢ TIẾNG ANH 9 THEO ĐƠN VỊ BÀI HỌC - GLOBAL SUCCESS - CẢ NĂM (TỪ...
Nguyen Thanh Tu Collection
 
The role of wall art in interior designing
The role of wall art in interior designingThe role of wall art in interior designing
The role of wall art in interior designing
meghaark2110
 
Rock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian HistoryRock Art As a Source of Ancient Indian History
Rock Art As a Source of Ancient Indian History
Virag Sontakke
 
antiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidenceantiquity of writing in ancient India- literary & archaeological evidence
antiquity of writing in ancient India- literary & archaeological evidence
PrachiSontakke5
 
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
MCQ PHYSIOLOGY II (DR. NASIR MUSTAFA) MCQS)
Dr. Nasir Mustafa
 
Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)Myasthenia gravis (Neuromuscular disorder)
Myasthenia gravis (Neuromuscular disorder)
Mohamed Rizk Khodair
 
Cultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptxCultivation Practice of Onion in Nepal.pptx
Cultivation Practice of Onion in Nepal.pptx
UmeshTimilsina1
 
Form View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo SlidesForm View Attributes in Odoo 18 - Odoo Slides
Form View Attributes in Odoo 18 - Odoo Slides
Celine George
 
Myopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduateMyopathies (muscle disorders) for undergraduate
Myopathies (muscle disorders) for undergraduate
Mohamed Rizk Khodair
 
How to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 PurchaseHow to Manage Amounts in Local Currency in Odoo 18 Purchase
How to Manage Amounts in Local Currency in Odoo 18 Purchase
Celine George
 
What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)What is the Philosophy of Statistics? (and how I was drawn to it)
What is the Philosophy of Statistics? (and how I was drawn to it)
jemille6
 
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...Transform tomorrow: Master benefits analysis with Gen AI today webinar,  30 A...
Transform tomorrow: Master benefits analysis with Gen AI today webinar, 30 A...
Association for Project Management
 
Drugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdfDrugs in Anaesthesia and Intensive Care,.pdf
Drugs in Anaesthesia and Intensive Care,.pdf
crewot855
 

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
  翻译: