MODULE-3
Array
• An array is a collection of similar type of elements which has contiguous
memory location.
• Array in Java is index-based, the first element of the array is stored at the
0th index, 2nd element is stored on 1st index and so on.
Advantages
• Code Optimization: It makes the code optimized, we can retrieve or sort
the data efficiently.
• Random access: We can get any data located at an index position.
Disadvantages
• Size Limit: We can store only the fixed size of elements in the array. It
doesn't grow its size at runtime. To solve this problem, collection
framework is used in Java which grows automatically.
Array.pptx
Types of Array in java
There are two types of array.
• Single Dimensional Array
• Multidimensional Array
Single Dimensional Array in Java
Syntax to Declare an Array in Java
dataType[] arr; (or)
dataType []arr; (or)
dataType arr[];
int a[]={33,3,4,5};//declaration, instantiation and initialization
Example for 1-D Array
class Testarray{
public static void main(String args[]){
int a[]=new int[5];//declaration and instantiation
a[0]=10;//initialization
a[1]=20;
a[2]=70;
a[3]=40;
a[4]=50;
//traversing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
Example for 1-D Array
class Testarray1{
public static void main(String args[]){
int a[]={33,3,4,5};//declaration, instantiation and initialization
//printing array
for(int i=0;i<a.length;i++)//length is the property of array
System.out.println(a[i]);
}}
Multidimensional Array in Java
• Data is stored in row and column based index (also known as matrix
form).
Syntax to Declare Multidimensional Array in Java
• dataType[][] arrayRefVar; (or)
• dataType [][]arrayRefVar; (or)
• dataType arrayRefVar[][]; (or)
• dataType []arrayRefVar[];
Example to instantiate Multidimensional Array
in Java
int[][] arr=new int[3][3];//3 row and 3 column
Example to initialize Multidimensional Array in Javaarr[0][0]=1;
• arr[0][1]=2;
• arr[0][2]=3;
• arr[1][0]=4;
• arr[1][1]=5;
• arr[1][2]=6;
• arr[2][0]=7;
• arr[2][1]=8;
• arr[2][2]=9;
Example of Multidimensional Java Array
class Testarray3{
public static void main(String args[]){
//declaring and initializing 2D array
int arr[][]={{1,2,3},{2,4,5},{4,4,5}};
//printing 2D array
for(int i=0;i<3;i++){
for(int j=0;j<3;j++){
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}}
Advanced for loop
For-each Loop for Java Array
• Print the Java array using for-each loop.
• The Java for-each loop prints the array elements one by one.
• It holds an array element in a variable, then executes the body of the
loop.
• The syntax of the for-each loop is given below:
for(data_type variable:array){
//body of the loop
}
Example
Java Program to print the array elements using for-each loop
class Testarray1{
public static void main(String args[]){
int arr[]={33,3,4,5};
//printing array using for-each loop
for(int i:arr)
System.out.println(i);
}}
Output
Output:
33
3
4
5
Advantage
• It makes the code more readable.
• It eliminates the possibility of programming errors.

More Related Content

PPTX
OOPs with java
PPTX
Java Programming
PPTX
ARRAYS.pptx
PDF
Arrays in Java
PPTX
Java Array String
PDF
Arrays in java
PPTX
the array, which stores a fixed-size sequential collection of elements of the...
OOPs with java
Java Programming
ARRAYS.pptx
Arrays in Java
Java Array String
Arrays in java
the array, which stores a fixed-size sequential collection of elements of the...

Similar to Array.pptx (20)

PPTX
Arrays in java.pptx
PPSX
dizital pods session 6-arrays.ppsx
PPTX
dizital pods session 6-arrays.pptx
PPTX
Arrays in programming
PPT
L10 array
PDF
Learn Java Part 9
PPTX
6_Array.pptx
PPTX
JAVA WORKSHOP(DAY 3) 1234567889999999.pptx
PPTX
Arrays in Reading.pptx
PDF
Java arrays
PPTX
Arrays in Data Structure and Algorithm
PPTX
Lecture 7 arrays
PPTX
Operators
PDF
Java chapter 6 - Arrays -syntax and use
PPTX
Chapter6 (4) (1).pptx plog fix down more
PDF
Class notes(week 4) on arrays and strings
DOCX
Class notes(week 4) on arrays and strings
PDF
Java Arrays
DOCX
Java R20 - UNIT-3.docx
PPTX
Java arrays
Arrays in java.pptx
dizital pods session 6-arrays.ppsx
dizital pods session 6-arrays.pptx
Arrays in programming
L10 array
Learn Java Part 9
6_Array.pptx
JAVA WORKSHOP(DAY 3) 1234567889999999.pptx
Arrays in Reading.pptx
Java arrays
Arrays in Data Structure and Algorithm
Lecture 7 arrays
Operators
Java chapter 6 - Arrays -syntax and use
Chapter6 (4) (1).pptx plog fix down more
Class notes(week 4) on arrays and strings
Class notes(week 4) on arrays and strings
Java Arrays
Java R20 - UNIT-3.docx
Java arrays
Ad

Recently uploaded (20)

PDF
Implantable Drug Delivery System_NDDS_BPHARMACY__SEM VII_PCI .pdf
PPTX
CN_Unite_1 AI&DS ENGGERING SPPU PUNE UNIVERSITY
PDF
Design Guidelines and solutions for Plastics parts
PPTX
Petroleum Refining & Petrochemicals.pptx
PPTX
Software Engineering and software moduleing
PDF
August -2025_Top10 Read_Articles_ijait.pdf
PDF
Exploratory_Data_Analysis_Fundamentals.pdf
PPTX
CyberSecurity Mobile and Wireless Devices
PDF
Soil Improvement Techniques Note - Rabbi
PDF
Computer organization and architecuture Digital Notes....pdf
PPTX
Measurement Uncertainty and Measurement System analysis
PPTX
Module 8- Technological and Communication Skills.pptx
PDF
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
PPTX
Amdahl’s law is explained in the above power point presentations
PPTX
Principal presentation for NAAC (1).pptx
PPTX
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
PDF
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PPTX
"Array and Linked List in Data Structures with Types, Operations, Implementat...
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
Implantable Drug Delivery System_NDDS_BPHARMACY__SEM VII_PCI .pdf
CN_Unite_1 AI&DS ENGGERING SPPU PUNE UNIVERSITY
Design Guidelines and solutions for Plastics parts
Petroleum Refining & Petrochemicals.pptx
Software Engineering and software moduleing
August -2025_Top10 Read_Articles_ijait.pdf
Exploratory_Data_Analysis_Fundamentals.pdf
CyberSecurity Mobile and Wireless Devices
Soil Improvement Techniques Note - Rabbi
Computer organization and architecuture Digital Notes....pdf
Measurement Uncertainty and Measurement System analysis
Module 8- Technological and Communication Skills.pptx
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
Amdahl’s law is explained in the above power point presentations
Principal presentation for NAAC (1).pptx
A Brief Introduction to IoT- Smart Objects: The "Things" in IoT
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
"Array and Linked List in Data Structures with Types, Operations, Implementat...
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
Ad

Array.pptx

  • 2. Array • An array is a collection of similar type of elements which has contiguous memory location. • Array in Java is index-based, the first element of the array is stored at the 0th index, 2nd element is stored on 1st index and so on. Advantages • Code Optimization: It makes the code optimized, we can retrieve or sort the data efficiently. • Random access: We can get any data located at an index position. Disadvantages • Size Limit: We can store only the fixed size of elements in the array. It doesn't grow its size at runtime. To solve this problem, collection framework is used in Java which grows automatically.
  • 4. Types of Array in java There are two types of array. • Single Dimensional Array • Multidimensional Array
  • 5. Single Dimensional Array in Java Syntax to Declare an Array in Java dataType[] arr; (or) dataType []arr; (or) dataType arr[]; int a[]={33,3,4,5};//declaration, instantiation and initialization
  • 6. Example for 1-D Array class Testarray{ public static void main(String args[]){ int a[]=new int[5];//declaration and instantiation a[0]=10;//initialization a[1]=20; a[2]=70; a[3]=40; a[4]=50; //traversing array for(int i=0;i<a.length;i++)//length is the property of array System.out.println(a[i]); }}
  • 7. Example for 1-D Array class Testarray1{ public static void main(String args[]){ int a[]={33,3,4,5};//declaration, instantiation and initialization //printing array for(int i=0;i<a.length;i++)//length is the property of array System.out.println(a[i]); }}
  • 8. Multidimensional Array in Java • Data is stored in row and column based index (also known as matrix form). Syntax to Declare Multidimensional Array in Java • dataType[][] arrayRefVar; (or) • dataType [][]arrayRefVar; (or) • dataType arrayRefVar[][]; (or) • dataType []arrayRefVar[];
  • 9. Example to instantiate Multidimensional Array in Java int[][] arr=new int[3][3];//3 row and 3 column Example to initialize Multidimensional Array in Javaarr[0][0]=1; • arr[0][1]=2; • arr[0][2]=3; • arr[1][0]=4; • arr[1][1]=5; • arr[1][2]=6; • arr[2][0]=7; • arr[2][1]=8; • arr[2][2]=9;
  • 10. Example of Multidimensional Java Array class Testarray3{ public static void main(String args[]){ //declaring and initializing 2D array int arr[][]={{1,2,3},{2,4,5},{4,4,5}}; //printing 2D array for(int i=0;i<3;i++){ for(int j=0;j<3;j++){ System.out.print(arr[i][j]+" "); } System.out.println(); } }}
  • 12. For-each Loop for Java Array • Print the Java array using for-each loop. • The Java for-each loop prints the array elements one by one. • It holds an array element in a variable, then executes the body of the loop. • The syntax of the for-each loop is given below: for(data_type variable:array){ //body of the loop }
  • 13. Example Java Program to print the array elements using for-each loop class Testarray1{ public static void main(String args[]){ int arr[]={33,3,4,5}; //printing array using for-each loop for(int i:arr) System.out.println(i); }}
  • 15. Advantage • It makes the code more readable. • It eliminates the possibility of programming errors.