SlideShare a Scribd company logo
Arrays in Java 
Arrays in Java are like arrays in C/C++ except in… 
• Arrays are not only set of homogeneous data types stored in 
contiguous memory locations, but they are set of reference types 
(not necessarily homogeneous in direct hierarchy) also. 
• Java arrays provide you with one final property length that helps 
you find out the length of the array without any extra coding 
• Arrays in java are treated as objects of type java.lang.Object class 
and once you allocate memory to an array, the array elements get 
filled up with the default value of the corresponding type ( 0 for 
Numbers, ‘u0000’ for char, false for boolean and null for any 
reference type. 
• Arrays are reusable. 
Now there are 4 ways to define an array which we shall see in the next 
slide…. [Contd.] 
sohamsengupta@yahoo.com1
Four Ways To define an Array 
int x[]; // declare an int array 
x=new int[10]; // allocate 
memeory 
The first line declares an array of 
type int. Not recommended by 
experts 
The second line allocates memory for 
10 elements with all initialized to 0 
int x[]=new int[10]; This is nothing but a merged version 
of the above. Not recommended by 
experts. 
sohamsengupta@yahoo.com2 
int[] x; 
x=new int[10]; 
The first line declares an array of 
type int. This is recommended. 
The second line allocates memory for 
10 elements with all initialized to 0 
int [] x=new int[10]; It’s a merged version of the above 
and the this is recommended.
Inline declaration and initialization of array 
• int[] x={719,38, 940, 8}; 
Is OK. It contains the 
following elements and 
size 4 
• But the following is 
sohamsengupta@yahoo.com3 
wrong… 
int[] x; 
x={719,38, 940, 8}; // Error 
• But to make it OK, do 
this… 
int[] x; 
x=new int[]{719,38, 940, 8}; 
• byte[] x=new byte[6]; 
for(int i=0;i<6;i++){ 
// codes go here 
} BAD Practice 
• byte[] x=new byte[6]; 
for(int i=0;i<x.length;i++){ 
// codes go here 
} GOOD Practice
Anonymous & Reusable Arrays 
• Look at the following code… 
System.out.println(new int[]{0,1,2,3,4}.length); 
This gives an output 5 as this is an array which has no 
name. 
• int[] x={1,2,3,4,5,6,7}; 
// codes go here 
x=new int[90]; // the previous elements are lost 
Thus arrays can be reused 
sohamsengupta@yahoo.com4
Arrays as return type of methods 
• To return some data rather than just printing, 
enhances the flexibility of the code. 
• The following method returns an array containing first n natural numbers 
• int[] getFirstNNumbers(int n){ 
int[] retVal=new int[n]; // allocating memory for n int numbers 
for(int k=0;k<retVal.length;k++){ // iterate n times i.e = retVal.length 
retVal[k]=k+1; // assigning the values, i.e. 1,2,3,4 so on 
} 
return retVal; // returns the array 
sohamsengupta@yahoo.com5 
}
2-D Arrays in Java 
• Java deals multi dimensional arrays as Array of Arrays. 
• We can declare a 2-D int array as… 
1. int[][] x=new int[4][3]; // 4 rows & 3 cols per row 
2. int[][] x=new int[4][]; // 4 rows and cols to be dynamically alocated 
the size 
x[0]=new int[5]; // means 5 cols in 0-th row 
x[2]=new int[3]; // means 3 cols in 2-th row (3rd row) 
3. int[][] x=new int[][5]; // Error; u can’t fix-up cols size without rows. 
4. int[][] x={{1,2,3},{4,5,6,7},}; // first row has 3 cols where as 
second row has 4 cols 
5. In general, for a 2-D array myArray, myArray.length is the number 
of rows and myArray[i].length is the number of cols in the i-th row 
6. For any array x, x.length=90; is WRONG since length is final 
sohamsengupta@yahoo.com6
Miscellany 
sohamsengupta@yahoo.com7 
• Code snippet: 
int[][] x; 
int[] t={1,2}; 
x=new int[][]{t, new int[]{1,2,3},{4,5,6,7},new int[6]}; 
System.out.println(x[0].length); 
System.out.println(x[1].length); 
• More on Arrays in the LAB Session 
• On to LAB with a fresh mind

More Related Content

What's hot

Array in c#
Array in c#Array in c#
Array in c#
Prem Kumar Badri
 
L10 array
L10 arrayL10 array
L10 array
teach4uin
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
أحمد محمد
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
topu93
 
Arrays
ArraysArrays
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
Awais Alam
 
Array
ArrayArray
Array
PRN USM
 
arrays of structures
arrays of structuresarrays of structures
arrays of structures
arushi bhatnagar
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
Maulen Bale
 
Arrays
ArraysArrays
Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive
Raj Naik
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
طارق بالحارث
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsAakash deep Singhal
 
Array
ArrayArray
Array Presentation
Array PresentationArray Presentation
Array Presentation
Deep Prajapati Microplacer
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
Hareem Naz
 

What's hot (20)

Array in c#
Array in c#Array in c#
Array in c#
 
L10 array
L10 arrayL10 array
L10 array
 
C++ Arrays
C++ ArraysC++ Arrays
C++ Arrays
 
Presentation on array
Presentation on array Presentation on array
Presentation on array
 
Arrays
ArraysArrays
Arrays
 
Arrays In C++
Arrays In C++Arrays In C++
Arrays In C++
 
Array
ArrayArray
Array
 
arrays of structures
arrays of structuresarrays of structures
arrays of structures
 
Data Structure Midterm Lesson Arrays
Data Structure Midterm Lesson ArraysData Structure Midterm Lesson Arrays
Data Structure Midterm Lesson Arrays
 
Arrays
ArraysArrays
Arrays
 
Arrays
ArraysArrays
Arrays
 
Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive Data Types - Premetive and Non Premetive
Data Types - Premetive and Non Premetive
 
Lecture 2a arrays
Lecture 2a arraysLecture 2a arrays
Lecture 2a arrays
 
Array
ArrayArray
Array
 
C++ programming (Array)
C++ programming (Array)C++ programming (Array)
C++ programming (Array)
 
Array lecture
Array lectureArray lecture
Array lecture
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Array
ArrayArray
Array
 
Array Presentation
Array PresentationArray Presentation
Array Presentation
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 

Similar to Core java day2

6 arrays injava
6 arrays injava6 arrays injava
6 arrays injavairdginfo
 
Arrays
ArraysArrays
Arrays
swathi reddy
 
Lecture 6
Lecture 6Lecture 6
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
eShikshak
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In Generalmartha leon
 
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
berekethailu2
 
javaArrays.pptx
javaArrays.pptxjavaArrays.pptx
javaArrays.pptx
AshishNayyar11
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
Eng Teong Cheah
 
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxLecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptx
ShahinAhmed49
 
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
 
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 in programming
Arrays in programmingArrays in programming
Arrays in programming
TaseerRao
 
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
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and types
Daman Toor
 
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
 
Arrays
ArraysArrays
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptx
Mrhaider4
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
Ashim Lamichhane
 

Similar to Core java day2 (20)

6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
Arrays
ArraysArrays
Arrays
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Mesics lecture 8 arrays in 'c'
Mesics lecture 8   arrays in 'c'Mesics lecture 8   arrays in 'c'
Mesics lecture 8 arrays in 'c'
 
Arrays In General
Arrays In GeneralArrays In General
Arrays In General
 
Chapter-Five.pptx
Chapter-Five.pptxChapter-Five.pptx
Chapter-Five.pptx
 
javaArrays.pptx
javaArrays.pptxjavaArrays.pptx
javaArrays.pptx
 
Comp102 lec 8
Comp102   lec 8Comp102   lec 8
Comp102 lec 8
 
Learn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & ArraysLearn C# Programming - Nullables & Arrays
Learn C# Programming - Nullables & Arrays
 
Lecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptxLecture_3.5-Array_Type Conversion_Math Class.pptx
Lecture_3.5-Array_Type Conversion_Math Class.pptx
 
Algo>Arrays
Algo>ArraysAlgo>Arrays
Algo>Arrays
 
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
 
Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................Unit-2.Arrays and Strings.pptx.................
Unit-2.Arrays and Strings.pptx.................
 
Arrays in programming
Arrays in programmingArrays in programming
Arrays in programming
 
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
 
Identifiers, keywords and types
Identifiers, keywords and typesIdentifiers, keywords and types
Identifiers, keywords and types
 
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
 
Arrays
ArraysArrays
Arrays
 
Visual Programing basic lectures 7.pptx
Visual Programing basic lectures  7.pptxVisual Programing basic lectures  7.pptx
Visual Programing basic lectures 7.pptx
 
Unit 6. Arrays
Unit 6. ArraysUnit 6. Arrays
Unit 6. Arrays
 

More from Soham Sengupta

Spring method-level-secuirty
Spring method-level-secuirtySpring method-level-secuirty
Spring method-level-secuirty
Soham Sengupta
 
Spring security mvc-1
Spring security mvc-1Spring security mvc-1
Spring security mvc-1
Soham Sengupta
 
JavaScript event handling assignment
JavaScript  event handling assignment JavaScript  event handling assignment
JavaScript event handling assignment
Soham Sengupta
 
Networking assignment 2
Networking assignment 2Networking assignment 2
Networking assignment 2
Soham Sengupta
 
Networking assignment 1
Networking assignment 1Networking assignment 1
Networking assignment 1
Soham Sengupta
 
Sohams cryptography basics
Sohams cryptography basicsSohams cryptography basics
Sohams cryptography basics
Soham Sengupta
 
Network programming1
Network programming1Network programming1
Network programming1
Soham Sengupta
 
JSR-82 Bluetooth tutorial
JSR-82 Bluetooth tutorialJSR-82 Bluetooth tutorial
JSR-82 Bluetooth tutorial
Soham Sengupta
 
Xmpp and java
Xmpp and javaXmpp and java
Xmpp and java
Soham Sengupta
 
Core java day1
Core java day1Core java day1
Core java day1
Soham Sengupta
 
Core java day4
Core java day4Core java day4
Core java day4
Soham Sengupta
 
Core java day5
Core java day5Core java day5
Core java day5
Soham Sengupta
 
Exceptions
ExceptionsExceptions
Exceptions
Soham Sengupta
 
Java.lang.object
Java.lang.objectJava.lang.object
Java.lang.object
Soham Sengupta
 
Jsp1
Jsp1Jsp1
Soham web security
Soham web securitySoham web security
Soham web security
Soham Sengupta
 
Html tables and_javascript
Html tables and_javascriptHtml tables and_javascript
Html tables and_javascript
Soham Sengupta
 
Html javascript
Html javascriptHtml javascript
Html javascript
Soham Sengupta
 
Java script
Java scriptJava script
Java script
Soham Sengupta
 
Sohamsg ajax
Sohamsg ajaxSohamsg ajax
Sohamsg ajax
Soham Sengupta
 

More from Soham Sengupta (20)

Spring method-level-secuirty
Spring method-level-secuirtySpring method-level-secuirty
Spring method-level-secuirty
 
Spring security mvc-1
Spring security mvc-1Spring security mvc-1
Spring security mvc-1
 
JavaScript event handling assignment
JavaScript  event handling assignment JavaScript  event handling assignment
JavaScript event handling assignment
 
Networking assignment 2
Networking assignment 2Networking assignment 2
Networking assignment 2
 
Networking assignment 1
Networking assignment 1Networking assignment 1
Networking assignment 1
 
Sohams cryptography basics
Sohams cryptography basicsSohams cryptography basics
Sohams cryptography basics
 
Network programming1
Network programming1Network programming1
Network programming1
 
JSR-82 Bluetooth tutorial
JSR-82 Bluetooth tutorialJSR-82 Bluetooth tutorial
JSR-82 Bluetooth tutorial
 
Xmpp and java
Xmpp and javaXmpp and java
Xmpp and java
 
Core java day1
Core java day1Core java day1
Core java day1
 
Core java day4
Core java day4Core java day4
Core java day4
 
Core java day5
Core java day5Core java day5
Core java day5
 
Exceptions
ExceptionsExceptions
Exceptions
 
Java.lang.object
Java.lang.objectJava.lang.object
Java.lang.object
 
Jsp1
Jsp1Jsp1
Jsp1
 
Soham web security
Soham web securitySoham web security
Soham web security
 
Html tables and_javascript
Html tables and_javascriptHtml tables and_javascript
Html tables and_javascript
 
Html javascript
Html javascriptHtml javascript
Html javascript
 
Java script
Java scriptJava script
Java script
 
Sohamsg ajax
Sohamsg ajaxSohamsg ajax
Sohamsg ajax
 

Recently uploaded

In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
takuyayamamoto1800
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
XfilesPro
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
Ortus Solutions, Corp
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
Ortus Solutions, Corp
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
NaapbooksPrivateLimi
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
Peter Caitens
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
Globus
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
Globus
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Shahin Sheidaei
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
wottaspaceseo
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
Jelle | Nordend
 

Recently uploaded (20)

In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoamOpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
OpenFOAM solver for Helmholtz equation, helmholtzFoam / helmholtzBubbleFoam
 
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
How Does XfilesPro Ensure Security While Sharing Documents in Salesforce?
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024BoxLang: Review our Visionary Licenses of 2024
BoxLang: Review our Visionary Licenses of 2024
 
Visitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.appVisitor Management System in India- Vizman.app
Visitor Management System in India- Vizman.app
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
First Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User EndpointsFirst Steps with Globus Compute Multi-User Endpoints
First Steps with Globus Compute Multi-User Endpoints
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
Gamify Your Mind; The Secret Sauce to Delivering Success, Continuously Improv...
 
SOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBrokerSOCRadar Research Team: Latest Activities of IntelBroker
SOCRadar Research Team: Latest Activities of IntelBroker
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
How Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptxHow Recreation Management Software Can Streamline Your Operations.pptx
How Recreation Management Software Can Streamline Your Operations.pptx
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
De mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FMEDe mooiste recreatieve routes ontdekken met RouteYou en FME
De mooiste recreatieve routes ontdekken met RouteYou en FME
 

Core java day2

  • 1. Arrays in Java Arrays in Java are like arrays in C/C++ except in… • Arrays are not only set of homogeneous data types stored in contiguous memory locations, but they are set of reference types (not necessarily homogeneous in direct hierarchy) also. • Java arrays provide you with one final property length that helps you find out the length of the array without any extra coding • Arrays in java are treated as objects of type java.lang.Object class and once you allocate memory to an array, the array elements get filled up with the default value of the corresponding type ( 0 for Numbers, ‘u0000’ for char, false for boolean and null for any reference type. • Arrays are reusable. Now there are 4 ways to define an array which we shall see in the next slide…. [Contd.] sohamsengupta@yahoo.com1
  • 2. Four Ways To define an Array int x[]; // declare an int array x=new int[10]; // allocate memeory The first line declares an array of type int. Not recommended by experts The second line allocates memory for 10 elements with all initialized to 0 int x[]=new int[10]; This is nothing but a merged version of the above. Not recommended by experts. sohamsengupta@yahoo.com2 int[] x; x=new int[10]; The first line declares an array of type int. This is recommended. The second line allocates memory for 10 elements with all initialized to 0 int [] x=new int[10]; It’s a merged version of the above and the this is recommended.
  • 3. Inline declaration and initialization of array • int[] x={719,38, 940, 8}; Is OK. It contains the following elements and size 4 • But the following is sohamsengupta@yahoo.com3 wrong… int[] x; x={719,38, 940, 8}; // Error • But to make it OK, do this… int[] x; x=new int[]{719,38, 940, 8}; • byte[] x=new byte[6]; for(int i=0;i<6;i++){ // codes go here } BAD Practice • byte[] x=new byte[6]; for(int i=0;i<x.length;i++){ // codes go here } GOOD Practice
  • 4. Anonymous & Reusable Arrays • Look at the following code… System.out.println(new int[]{0,1,2,3,4}.length); This gives an output 5 as this is an array which has no name. • int[] x={1,2,3,4,5,6,7}; // codes go here x=new int[90]; // the previous elements are lost Thus arrays can be reused sohamsengupta@yahoo.com4
  • 5. Arrays as return type of methods • To return some data rather than just printing, enhances the flexibility of the code. • The following method returns an array containing first n natural numbers • int[] getFirstNNumbers(int n){ int[] retVal=new int[n]; // allocating memory for n int numbers for(int k=0;k<retVal.length;k++){ // iterate n times i.e = retVal.length retVal[k]=k+1; // assigning the values, i.e. 1,2,3,4 so on } return retVal; // returns the array sohamsengupta@yahoo.com5 }
  • 6. 2-D Arrays in Java • Java deals multi dimensional arrays as Array of Arrays. • We can declare a 2-D int array as… 1. int[][] x=new int[4][3]; // 4 rows & 3 cols per row 2. int[][] x=new int[4][]; // 4 rows and cols to be dynamically alocated the size x[0]=new int[5]; // means 5 cols in 0-th row x[2]=new int[3]; // means 3 cols in 2-th row (3rd row) 3. int[][] x=new int[][5]; // Error; u can’t fix-up cols size without rows. 4. int[][] x={{1,2,3},{4,5,6,7},}; // first row has 3 cols where as second row has 4 cols 5. In general, for a 2-D array myArray, myArray.length is the number of rows and myArray[i].length is the number of cols in the i-th row 6. For any array x, x.length=90; is WRONG since length is final sohamsengupta@yahoo.com6
  • 7. Miscellany sohamsengupta@yahoo.com7 • Code snippet: int[][] x; int[] t={1,2}; x=new int[][]{t, new int[]{1,2,3},{4,5,6,7},new int[6]}; System.out.println(x[0].length); System.out.println(x[1].length); • More on Arrays in the LAB Session • On to LAB with a fresh mind