SlideShare a Scribd company logo
Java & JEE Training
Session 4 – Handling Arrays in Java
Page 1Classification: Restricted
Agenda
• Autoboxing and Unboxing in Java
• Handling Arrays
Autoboxing and Unboxing in Java
Page 3Classification: Restricted
Autoboxing and Unboxing in Java
• Autoboxing is the automatic conversion that the Java compiler makes
between the primitive types and their corresponding object wrapper
classes. For example, converting an int to an Integer, a double to a Double,
and so on.
• If the conversion goes the other way, this is called unboxing.
• Introduced in JDK 5
Page 4Classification: Restricted
Autoboxing and Unboxing in Java
Handling Arrays
Page 6Classification: Restricted
Arrays
• We need a way to store and manipulate huge quantities of data.
• “An array is an indexed sequence of values of the same type.”
Examples.
• 52 playing cards in a deck.
• 5 thousand undergrads at Princeton.
• 1 million characters in a book.
• 10 million audio samples in an MP3 file.
• 4 billion nucleotides in a DNA strand.
• 73 billion Google queries per year.
• 50 trillion cells in the human body.
Page 7Classification: Restricted
Goal: Many variables of the same type
Page 8Classification: Restricted
Goal: Many variables of the same type
Page 9Classification: Restricted
Arrays in Java
Java has special language support for arrays.
• To make an array: declare, create, and initialize it.
• To access element i of array named a, use a[i].
• Array indices start at 0.
Compact alternative.
• Declare, create, and initialize in one statement.
• Default initialization: all numbers automatically set to zero.
Page 10Classification: Restricted
Declaring Arrays – Two ways
Page 11Classification: Restricted
Exercise: Vector dot product
Dot product. Given two vectors x[] and y[] of length N, their dot product is
the sum of the products of their corresponding components.
Page 12Classification: Restricted
Array Processing Examples
Page 13Classification: Restricted
Example: Card shuffling algorithm
Goal. Given an array, rearrange its elements in random order.
• Shuffling algorithm.
• In iteration i, pick random card from deck[i] through deck[N-1], with each
card equally likely.
• Exchange it with deck[i].
Page 14Classification: Restricted
Two Dimensional Arrays
Two-dimensional arrays.
• Table of data for each experiment and outcome.
• Table of grades for each student and assignments.
• Table of grayscale values for each pixel in a 2D image.
Mathematical abstraction. Matrix.
Java abstraction. 2D array.
Page 15Classification: Restricted
Two Dimensional Arrays in Java
• Array access. Use a[i][j] to access element in row i and column j.
• Zero-based indexing. Row and column indices start at 0.
• a[i] is the i’th row, which is a one dimensional array here.
Page 16Classification: Restricted
Initialize 2D array : Inline initialization
Page 17Classification: Restricted
Example: Matrix Addition
Page 18Classification: Restricted
Example: Matrix Multiplication
Page 19Classification: Restricted
Example: Using arrays to simplify repetitive code
How to simplify writing the above code?
Page 20Classification: Restricted
Example: Using arrays to simplify repetitive code
Page 21Classification: Restricted
Default Initialization: Class / instance variable & Arrays
Each class variable, instance variable, or array component is initialized with a
default value when it is created:
Page 22Classification: Restricted
Exercise: What is the output? Why?
Page 23Classification: Restricted
Exercise: What is the output? Why?
Attempts to create an array of size n^4 for n = 1000.
This program compiles cleans.
When n is 1000, it leads to the following error
java.lang.NegativeArraySizeException
because 1000^4 overflows an int and results in a negative integer.
When n is 200, it leads to the following error
java.lang.OutOfMemoryError: Requested array size exceeds VM limit
Page 24Classification: Restricted
Exercise: What is the output?
What is wrong with the following code fragment?
Page 25Classification: Restricted
Exercise: What is the output?
What is wrong with the following code fragment?
It does not allocate memory for a[] with new. The code results in a variable
might not have been initialized compile-time error.
Page 26Classification: Restricted
Exercise: What is the output?
Page 27Classification: Restricted
Exercise: What is the output?
It prints false. The == operator compares whether the (memory addresses
of the) two arrays are identical, not whether their corresponding values are
equal.
Page 28Classification: Restricted
What is the output?
• What happens when you try to compile a program with the following
statement?
int[] a = new int[-17];
Page 29Classification: Restricted
What is the output?
• What happens when you try to compile a program with the following
statement?
int[] a = new int[-17];
It compiles cleanly, but throws a java.lang.NegativeArraySizeException
when you execute it.
Page 30Classification: Restricted
What is the output?
• Suppose that b[] is an array of 100 elements, with all entries initialized to 0,
and that a[] is an array of N elements, each of which is an integer between
0 and 99. What is the effect of the following loop?
Page 31Classification: Restricted
Iterating through elements in an Array – Two ways
• for Loop
• foreach Loop (introduced in Java 1.5)
Page 32Classification: Restricted
Arrays – Sorting and Searching
• java.util package has API for search and sort
• Arrays.sort() –
• Default sorting is ascending order
• But this can be used with sorting order Collections.reverseOrder()
which can be used for sorting in descending order, but can be used
only with Object type and not primitive type
• Arrays.binarySearch()
Page 33Classification: Restricted
Exercise
• Given an array of sorted elements, search for the element requested.
• Given a 2D array that is sorted column wise and row wise, write an
algorithm to find if an element exists in the matrix or not. Keep the
temporal complexity of the algorithm in mind.
Page 34Classification: Restricted
Topics to be covered in next session
• Memory Allocation & Garbage Collection
• Strings in Java
Page 35Classification: Restricted
Thank You!

More Related Content

PPSX
Practice Session
PPTX
Session 06 - Practice Session
PPSX
Arrays in Java
PPTX
Session 03 - Elements of Java Language
PPTX
Session 05 - Strings in Java
PPTX
Session 10 - OOP with Java - Abstract Classes and Interfaces
PPTX
Session 01 - Introduction to Java
PPSX
Strings in Java
Practice Session
Session 06 - Practice Session
Arrays in Java
Session 03 - Elements of Java Language
Session 05 - Strings in Java
Session 10 - OOP with Java - Abstract Classes and Interfaces
Session 01 - Introduction to Java
Strings in Java

What's hot (9)

PPTX
Session 02 - Elements of Java Language
PPTX
Session 07 - Intro to Object Oriented Programming with Java
PPTX
Session 11 - OOP's with Java - Packaging and Access Modifiers
PPSX
Review Session - Part -2
PPTX
Session 18 - Review Session and Attending Java Interviews
PPTX
Session 14 - Object Class
PPT
10. Introduction to Datastructure
PDF
From DOT to Dotty
PPT
Polymorphism
Session 02 - Elements of Java Language
Session 07 - Intro to Object Oriented Programming with Java
Session 11 - OOP's with Java - Packaging and Access Modifiers
Review Session - Part -2
Session 18 - Review Session and Attending Java Interviews
Session 14 - Object Class
10. Introduction to Datastructure
From DOT to Dotty
Polymorphism
Ad

Similar to Session 04 - Arrays in Java (20)

PPTX
Data Handling and Function
PPTX
Lec16 - Autoencoders.pptx
PPTX
DL-unite4-Autoencoders.pptx..............
PPTX
Learning core java
PPTX
Java ce241
PDF
Set Transfomer: A Framework for Attention-based Permutaion-Invariant Neural N...
PPTX
DOCX
Mcs 024 assignment solution (2020-21)
PPTX
Java 102 intro to object-oriented programming in java
ODP
Best practices in Java
PDF
findbugs Bernhard Merkle
PPTX
Review Session and Attending Java Interviews
PDF
Core Java An Integrated Approach 2nd R Nageswara Rao
PPTX
241007_JH_labseminar[LightGCN: Simplifying and Powering Graph Convolution Net...
PDF
Data mining with Weka
PDF
Lifecycle of a JIT compiled code
PPTX
StackNet Meta-Modelling framework
PPTX
JAVA WORKSHOP(DAY 3) 1234567889999999.pptx
PPTX
Wits presentation 6_28072015
DOCX
Mcs 024 assignment solution (2020-21)
Data Handling and Function
Lec16 - Autoencoders.pptx
DL-unite4-Autoencoders.pptx..............
Learning core java
Java ce241
Set Transfomer: A Framework for Attention-based Permutaion-Invariant Neural N...
Mcs 024 assignment solution (2020-21)
Java 102 intro to object-oriented programming in java
Best practices in Java
findbugs Bernhard Merkle
Review Session and Attending Java Interviews
Core Java An Integrated Approach 2nd R Nageswara Rao
241007_JH_labseminar[LightGCN: Simplifying and Powering Graph Convolution Net...
Data mining with Weka
Lifecycle of a JIT compiled code
StackNet Meta-Modelling framework
JAVA WORKSHOP(DAY 3) 1234567889999999.pptx
Wits presentation 6_28072015
Mcs 024 assignment solution (2020-21)
Ad

More from PawanMM (20)

PPTX
Session 48 - JS, JSON and AJAX
PPTX
Session 46 - Spring - Part 4 - Spring MVC
PPTX
Session 45 - Spring - Part 3 - AOP
PPTX
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
PPTX
Session 43 - Spring - Part 1 - IoC DI Beans
PPTX
Session 42 - Struts 2 Hibernate Integration
PPTX
Session 41 - Struts 2 Introduction
PPTX
Session 40 - Hibernate - Part 2
PPTX
Session 39 - Hibernate - Part 1
PPTX
Session 38 - Core Java (New Features) - Part 1
PPTX
Session 37 - JSP - Part 2 (final)
PPTX
Session 36 - JSP - Part 1
PPTX
Session 35 - Design Patterns
PPTX
Session 34 - JDBC Best Practices, Introduction to Design Patterns
PPTX
Session 33 - Session Management using other Techniques
PPTX
Session 32 - Session Management using Cookies
PPTX
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
PPTX
Session 30 - Servlets - Part 6
PPTX
Session 29 - Servlets - Part 5
PPTX
Session 28 - Servlets - Part 4
Session 48 - JS, JSON and AJAX
Session 46 - Spring - Part 4 - Spring MVC
Session 45 - Spring - Part 3 - AOP
Session 44 - Spring - Part 2 - Autowiring, Annotations, Java based Configuration
Session 43 - Spring - Part 1 - IoC DI Beans
Session 42 - Struts 2 Hibernate Integration
Session 41 - Struts 2 Introduction
Session 40 - Hibernate - Part 2
Session 39 - Hibernate - Part 1
Session 38 - Core Java (New Features) - Part 1
Session 37 - JSP - Part 2 (final)
Session 36 - JSP - Part 1
Session 35 - Design Patterns
Session 34 - JDBC Best Practices, Introduction to Design Patterns
Session 33 - Session Management using other Techniques
Session 32 - Session Management using Cookies
Session 31 - Session Management, Best Practices, Design Patterns in Web Apps
Session 30 - Servlets - Part 6
Session 29 - Servlets - Part 5
Session 28 - Servlets - Part 4

Recently uploaded (20)

PDF
Machine learning based COVID-19 study performance prediction
PDF
cuic standard and advanced reporting.pdf
PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Electronic commerce courselecture one. Pdf
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
DOCX
The AUB Centre for AI in Media Proposal.docx
PPTX
Programs and apps: productivity, graphics, security and other tools
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PPTX
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
PDF
MIND Revenue Release Quarter 2 2025 Press Release
Machine learning based COVID-19 study performance prediction
cuic standard and advanced reporting.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Electronic commerce courselecture one. Pdf
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Effective Security Operations Center (SOC) A Modern, Strategic, and Threat-In...
sap open course for s4hana steps from ECC to s4
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
The AUB Centre for AI in Media Proposal.docx
Programs and apps: productivity, graphics, security and other tools
Mobile App Security Testing_ A Comprehensive Guide.pdf
Network Security Unit 5.pdf for BCA BBA.
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
NewMind AI Weekly Chronicles - August'25 Week I
Digital-Transformation-Roadmap-for-Companies.pptx
Unlocking AI with Model Context Protocol (MCP)
Understanding_Digital_Forensics_Presentation.pptx
Detection-First SIEM: Rule Types, Dashboards, and Threat-Informed Strategy
MIND Revenue Release Quarter 2 2025 Press Release

Session 04 - Arrays in Java