SlideShare a Scribd company logo
Core Java Training
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
Strings in Java
PPSX
Elements of Java Language
PPSX
Introduction to Java
PDF
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
PDF
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
PPTX
Core java
PPSX
Collections - Maps
PDF
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...
Strings in Java
Elements of Java Language
Introduction to Java
What Is Java | Java Tutorial | Java Programming | Learn Java | Edureka
Java Training | Java Tutorial for Beginners | Java Programming | Java Certifi...
Core java
Collections - Maps
Java Tutorial | Java Programming Tutorial | Java Basics | Java Training | Edu...

What's hot (20)

PPTX
Core java complete ppt(note)
PPTX
Introduction to java
PPTX
Basic Concepts of OOPs (Object Oriented Programming in Java)
PPTX
Advance Java Topics (J2EE)
PPTX
JAVA AWT
PDF
Methods in Java
PPTX
Spring boot
PPTX
Introduction to java
PDF
Arrays in Java
PDF
Basic i/o & file handling in java
PPTX
Type casting in java
PDF
Introduction to java (revised)
PPTX
PPTX
Inner classes in java
PPT
9. Input Output in java
PPTX
Introduction to java
PPTX
core java
PPTX
Object Oriented Programming Concepts for beginners
Core java complete ppt(note)
Introduction to java
Basic Concepts of OOPs (Object Oriented Programming in Java)
Advance Java Topics (J2EE)
JAVA AWT
Methods in Java
Spring boot
Introduction to java
Arrays in Java
Basic i/o & file handling in java
Type casting in java
Introduction to java (revised)
Inner classes in java
9. Input Output in java
Introduction to java
core java
Object Oriented Programming Concepts for beginners
Ad

Similar to Arrays in Java (20)

PPTX
Session 04 - Arrays in Java
PPSX
Core Java Basics
PPTX
Learning core java
PPTX
Session 06 - Java Basics
PPTX
Session 38 - Core Java (New Features) - Part 1
PPT
02basics
PPTX
Collections Array list
PPTX
Introduction-to-Arrays-in-Java . Exploring array
PPTX
intro_java (1).pptx
PPT
Ap Power Point Chpt6
PDF
Java Script Sysllabus for the computer.pdf
PPSX
Review Session and Attending Java Interviews
PDF
(chapter 4) A Concise and Practical Introduction to Programming Algorithms in...
PPTX
Arrays in programming
PPSX
Review Session - Part -2
PPT
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
PPT
Arrays in Java Programming Language slides
PPTX
Java Jive 002.pptx
PPTX
Java fundamentals
PPSX
Core Java for Selenium
Session 04 - Arrays in Java
Core Java Basics
Learning core java
Session 06 - Java Basics
Session 38 - Core Java (New Features) - Part 1
02basics
Collections Array list
Introduction-to-Arrays-in-Java . Exploring array
intro_java (1).pptx
Ap Power Point Chpt6
Java Script Sysllabus for the computer.pdf
Review Session and Attending Java Interviews
(chapter 4) A Concise and Practical Introduction to Programming Algorithms in...
Arrays in programming
Review Session - Part -2
slidlecturlecturlecturlecturlecturlecturlecturlectures06.ppt
Arrays in Java Programming Language slides
Java Jive 002.pptx
Java fundamentals
Core Java for Selenium
Ad

More from Hitesh-Java (20)

PPSX
Spring - Part 4 - Spring MVC
PPSX
Spring - Part 3 - AOP
PPSX
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
PPSX
Spring - Part 1 - IoC, Di and Beans
PPSX
JSP - Part 2 (Final)
PPSX
JSP - Part 1
PPSX
Struts 2 - Hibernate Integration
PPSX
Struts 2 - Introduction
PPSX
Hibernate - Part 2
PPSX
Hibernate - Part 1
PPSX
JDBC Part - 2
PPSX
PPSX
Java IO, Serialization
PPSX
Inner Classes
PPSX
Collections - Lists, Sets
PPSX
Collections - Sorting, Comparing Basics
PPSX
Collections - Array List
PPSX
Object Class
PPSX
Exception Handling - Continued
PPSX
Exception Handling - Part 1
Spring - Part 4 - Spring MVC
Spring - Part 3 - AOP
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 1 - IoC, Di and Beans
JSP - Part 2 (Final)
JSP - Part 1
Struts 2 - Hibernate Integration
Struts 2 - Introduction
Hibernate - Part 2
Hibernate - Part 1
JDBC Part - 2
Java IO, Serialization
Inner Classes
Collections - Lists, Sets
Collections - Sorting, Comparing Basics
Collections - Array List
Object Class
Exception Handling - Continued
Exception Handling - Part 1

Recently uploaded (20)

PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
PDF
Empathic Computing: Creating Shared Understanding
PDF
Per capita expenditure prediction using model stacking based on satellite ima...
PDF
NewMind AI Weekly Chronicles - August'25 Week I
PDF
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
PDF
Machine learning based COVID-19 study performance prediction
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
CIFDAQ's Market Insight: SEC Turns Pro Crypto
PPTX
Understanding_Digital_Forensics_Presentation.pptx
PDF
Network Security Unit 5.pdf for BCA BBA.
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PPTX
MYSQL Presentation for SQL database connectivity
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PDF
Encapsulation_ Review paper, used for researhc scholars
PPTX
Cloud computing and distributed systems.
PDF
Review of recent advances in non-invasive hemoglobin estimation
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
Empathic Computing: Creating Shared Understanding
Per capita expenditure prediction using model stacking based on satellite ima...
NewMind AI Weekly Chronicles - August'25 Week I
Peak of Data & AI Encore- AI for Metadata and Smarter Workflows
Machine learning based COVID-19 study performance prediction
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
CIFDAQ's Market Insight: SEC Turns Pro Crypto
Understanding_Digital_Forensics_Presentation.pptx
Network Security Unit 5.pdf for BCA BBA.
Mobile App Security Testing_ A Comprehensive Guide.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Shreyas Phanse Resume: Experienced Backend Engineer | Java • Spring Boot • Ka...
Advanced methodologies resolving dimensionality complications for autism neur...
Diabetes mellitus diagnosis method based random forest with bat algorithm
MYSQL Presentation for SQL database connectivity
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Encapsulation_ Review paper, used for researhc scholars
Cloud computing and distributed systems.
Review of recent advances in non-invasive hemoglobin estimation

Arrays in Java