SlideShare a Scribd company logo
Programming in Java
Lecture 10: Arrays
By
Ravi Kant Sahu
Asst. Professor, LPU
Array
• Definition:
An array is a group/collection of variables of the
same type that are referred to by a common name.
• Arrays of any type can be created and may have one
or more dimensions.
• A specific element in an array is accessed by its index
(subscript).
• Examples:
• Collection of numbers
• Collection of names
• Collection of suffixes
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Array of numbers:
Array of names:
Array of suffixes:
Examples
10 23 863 8 229
ment tion ness ves
Sam Shanu Riya
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
One-Dimensional Arrays
• A one-dimensional array is a list of variables of same
type.
• The general form of a one-dimensional array
declaration is:
type var-name[ ]; array-var = new type[size];
or, type var-name[ ] = new type[size];
Example:
int num [] = new int [10];
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Declaration of array variable:
data-type variable-name[];
eg. int marks[];
This will declare an array named ‘marks’ of type ‘int’. But no memory is
allocated to the array.
Allocation of memory:
variable-name = new data-type[size];
eg. marks = new int[5];
This will allocate memory of 5 integers to the array ‘marks’ and it can store upto
5 integers in it. ‘new’ is a special operator that allocates memory.
Syntax
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Accessing elements in the array:
• Specific element in the array is accessed by specifying
name of the array followed the index of the element.
• All array indexes in Java start at zero.
variable-name[index] = value;
Example:
marks[0] = 10;
This will assign the value 10 to the 1st element in the array.
marks[2] = 863;;
This will assign the value 863 to the 3rd element in the array.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
STEP 1 : (Declaration)
int marks[];
marks  null
STEP 2: (Memory Allocation)
marks = new int[5];
marks 
marks[0] marks[1] marks[2] marks[3] marks[4]
STEP 3: (Accessing Elements)
marks[0] = 10;
marks 
marks[0] marks[1] marks[2] marks[3] marks[4]
Example
0 0 0 0 0
10 0 0 0 0
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• Size of an array can’t be changed after the array is created.
• Default values:
– zero (0) for numeric data types,
– u0000 for chars and
– false for boolean types
• Length of an array can be obtained as:
array_ref_var.length
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
class Demo_Array
{
public static void main(String args[])
{
int marks[];
marks = new int[3];
marks[0] = 10;
marks[1] = 35;
marks[2] = 84;
System.out.println(“Marks of 2nd student=” + marks[1]);
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• Arrays can store elements of the same data type. Hence an
int array CAN NOT store an element which is not an int.
• Though an element of a compatible type can be converted
to int and stored into the int array.
eg. marks[2] = (int) 22.5;
This will convert ‘22.5’ into the int part ‘22’ and store it into the 3rd place
in the int array ‘marks’.
• Array indexes start from zero. Hence ‘marks[index]’ refers
to the (index+1)th element in the array and ‘marks[size-1]’
refers to last element in the array.
Note
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Array Initialization
1. data Type [] array_ref_var = {value0, value1, …, value n};
2. data Type [] array_ref_var;
array_ref_var = {value0, value1, …,value n};
3. data Type [] array_ref_var = new data Type [n+1];
array_ref_var [0] = value 0;
array_ref_var [1] = value 1;
…
array_ref_var [n] = value n;
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
int[] v = new int[10];
int i = 7;
int j = 2;
int k = 4;
v[0] = 1;
v[i] = 5;
v[j] = v[i] + 3;
v[j+1] = v[i] + v[0];
v[v[j]] = 12;
System.out.println(v[2]);
v[k] = stdin.nextInt();
v 00 0 00 0 00 00
v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Example
int[] v = new int[10];
int i = 7;
int j = 2;
int k = 4;
v[0] = 1;
v[i] = 5;
v[j] = v[i] + 3;
v[j+1] = v[i] + v[0];
v[v[j]] = 12;
System.out.println(v[2]);
v[k] = stdin.nextInt();
v 01 0 00 0 00 00
v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]
Example
int[] v = new int[10];
int i = 7;
int j = 2;
int k = 4;
v[0] = 1;
v[i] = 5;
v[j] = v[i] + 3;
v[j+1] = v[i] + v[0];
v[v[j]] = 12;
System.out.println(v[2]);
v[k] = stdin.nextInt();
v 01 0 00 0 50 00
v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]
Example
int[] v = new int[10];
int i = 7;
int j = 2;
int k = 4;
v[0] = 1;
v[i] = 5;
v[j] = v[i] + 3;
v[j+1] = v[i] + v[0];
v[v[j]] = 12;
System.out.println(v[2]);
v[k] = stdin.nextInt();
v 81 0 00 0 50 00
v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]
Example
int[] v = new int[10];
int i = 7;
int j = 2;
int k = 4;
v[0] = 1;
v[i] = 5;
v[j] = v[i] + 3;
v[j+1] = v[i] + v[0];
v[v[j]] = 12;
System.out.println(v[2]);
v[k] = stdin.nextInt();
v 81 0 06 0 50 00
v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]
Example
int[] v = new int[10];
int i = 7;
int j = 2;
int k = 4;
v[0] = 1;
v[i] = 5;
v[j] = v[i] + 3;
v[j+1] = v[i] + v[0];
v[v[j]] = 12;
System.out.println(v[2]);
v[k] = stdin.nextInt();
v 81 0 06 0 50 012
v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]
Example
int[] v = new int[10];
int i = 7;
int j = 2;
int k = 4;
v[0] = 1;
v[i] = 5;
v[j] = v[i] + 3;
v[j+1] = v[i] + v[0];
v[v[j]] = 12;
System.out.println(v[2]);
v[k] = stdin.nextInt();
v 81 0 06 0 50 012
v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]
8 is displayed
Example
int[] v = new int[10];
int i = 7;
int j = 2;
int k = 4;
v[0] = 1;
v[i] = 5;
v[j] = v[i] + 3;
v[j+1] = v[i] + v[0];
v[v[j]] = 12;
System.out.println(v[2]);
v[k] = stdin.nextInt();
v 81 0 06 3 50 012
v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]
Suppose 3 is extracted
Multi-Dimensional Array
• Multidimensional arrays are arrays of arrays.
• Two-Dimensional arrays are used to represent a table or a
matrix.
• Creating Two-Dimensional Array:
int twoD[][] = new int[4][5];
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Conceptual View of 2-Dimensional Array
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
class TwoDimArr
{
public static void main(String args[])
{
int twoD[][]= new int[4][5];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<5; j++)
{
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++)
{
for(j=0; j<5; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
• When we allocate memory for a multidimensional
array, we need to only specify the memory for the
first (leftmost) dimension.
int twoD[][] = new int[4][];
• The other dimensions can be assigned manually.
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
// Manually allocate differing size second dimensions.
class TwoDAgain {
public static void main(String args[]) {
int twoD[][] = new int[4][];
twoD[0] = new int[1];
twoD[1] = new int[2];
twoD[2] = new int[3];
twoD[3] = new int[4];
int i, j, k = 0;
for(i=0; i<4; i++)
for(j=0; j<i+1; j++)
{
twoD[i][j] = k;
k++;
}
for(i=0; i<4; i++) {
for(j=0; j<i+1; j++)
System.out.print(twoD[i][j] + " ");
System.out.println();
}
}
}Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Initializing Multi-Dimensional Array
class Matrix {
public static void main(String args[]) {
double m[][] = {
{ 0*0, 1*0, 2*0, 3*0 },
{ 0*1, 1*1, 2*1, 3*1 },
{ 0*2, 1*2, 2*2, 3*2 },
{ 0*3, 1*3, 2*3, 3*3 }
};
int i, j;
for(i=0; i<4; i++) {
for(j=0; j<4; j++)
System.out.print(m[i][j] + " ");
System.out.println();
}
}
}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Alternative Array Declaration
type[ ] var-name;
• Example:
char twod1[][] = new char[3][4];
char[][] twod2 = new char[3][4];
• This alternative declaration form offers convenience
when declaring several arrays at the same time.
• Example: int[] nums, nums2, nums3;
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Array Cloning
• To actually create another array with its own values,
Java provides the clone()method.
• arr2 = arr1; (assignment)
is not equivalent to
arr2 = arr1.clone(); (cloning)
Example: ArrayCloning.java
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Array as Argument
• Arrays can be passed as an argument to any method.
Example:
void show ( int []) {}
public static void main(String arr[]) {}
void compareArray(int [] a, int [] b ){}
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Array as a return Type
• Arrays can also be returned as the return type of any
method
Example:
public int [] Result (int roll_No, int marks )
Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
Array

More Related Content

What's hot

Java practical
Java practicalJava practical
Java practical
william otto
 
Anonymous functions in JavaScript
Anonymous functions in JavaScriptAnonymous functions in JavaScript
Anonymous functions in JavaScript
Mohammed Sazid Al Rashid
 
Boxing & unboxing
Boxing & unboxingBoxing & unboxing
Boxing & unboxing
Larry Nung
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
Pavith Gunasekara
 
SQL
SQLSQL
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
Sony India Software Center
 
C# - Part 1
C# - Part 1C# - Part 1
C# - Part 1
Md. Mahedee Hasan
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
sinhacp
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
Svetlin Nakov
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)
Aijaz Ali Abro
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
Ram132
 
Java Swing
Java SwingJava Swing
Java Swing
Arkadeep Dey
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
Prem Kumar Badri
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
farwa waqar
 
07 java collection
07 java collection07 java collection
07 java collection
Abhishek Khune
 
C# File IO Operations
C# File IO OperationsC# File IO Operations
C# File IO Operations
Prem Kumar Badri
 
Java collections
Java collectionsJava collections
Java collections
Hamid Ghorbani
 

What's hot (20)

Java practical
Java practicalJava practical
Java practical
 
Anonymous functions in JavaScript
Anonymous functions in JavaScriptAnonymous functions in JavaScript
Anonymous functions in JavaScript
 
Boxing & unboxing
Boxing & unboxingBoxing & unboxing
Boxing & unboxing
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
SQL
SQLSQL
SQL
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Java keywords
Java keywordsJava keywords
Java keywords
 
Java input
Java inputJava input
Java input
 
C# - Part 1
C# - Part 1C# - Part 1
C# - Part 1
 
Aggregate functions
Aggregate functionsAggregate functions
Aggregate functions
 
Java Foundations: Methods
Java Foundations: MethodsJava Foundations: Methods
Java Foundations: Methods
 
Collections and its types in C# (with examples)
Collections and its types in C# (with examples)Collections and its types in C# (with examples)
Collections and its types in C# (with examples)
 
Core java concepts
Core java  conceptsCore java  concepts
Core java concepts
 
Generics
GenericsGenerics
Generics
 
Java Swing
Java SwingJava Swing
Java Swing
 
C# Inheritance
C# InheritanceC# Inheritance
C# Inheritance
 
introdution to SQL and SQL functions
introdution to SQL and SQL functionsintrodution to SQL and SQL functions
introdution to SQL and SQL functions
 
07 java collection
07 java collection07 java collection
07 java collection
 
C# File IO Operations
C# File IO OperationsC# File IO Operations
C# File IO Operations
 
Java collections
Java collectionsJava collections
Java collections
 

Viewers also liked

Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructorsRavi_Kant_Sahu
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variablesRavi_Kant_Sahu
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)Ravi_Kant_Sahu
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in JavaRavi_Kant_Sahu
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi Kant Sahu
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
Ravi Kant Sahu
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in JavaRavi_Kant_Sahu
 

Viewers also liked (18)

Keywords and classes
Keywords and classesKeywords and classes
Keywords and classes
 
Methods and constructors
Methods and constructorsMethods and constructors
Methods and constructors
 
Basic IO
Basic IOBasic IO
Basic IO
 
L2 datatypes and variables
L2 datatypes and variablesL2 datatypes and variables
L2 datatypes and variables
 
Exception handling
Exception handlingException handling
Exception handling
 
String handling(string buffer class)
String handling(string buffer class)String handling(string buffer class)
String handling(string buffer class)
 
Wrapper classes
Wrapper classesWrapper classes
Wrapper classes
 
Event handling
Event handlingEvent handling
Event handling
 
Packages
PackagesPackages
Packages
 
Multi threading
Multi threadingMulti threading
Multi threading
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Classes and Nested Classes in Java
Classes and Nested Classes in JavaClasses and Nested Classes in Java
Classes and Nested Classes in Java
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
Inheritance
InheritanceInheritance
Inheritance
 
Applets
AppletsApplets
Applets
 
Gui programming (awt)
Gui programming (awt)Gui programming (awt)
Gui programming (awt)
 
Introduction to Java Programming
Introduction to Java ProgrammingIntroduction to Java Programming
Introduction to Java Programming
 
Control structures in Java
Control structures in JavaControl structures in Java
Control structures in Java
 

Similar to Array

Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays Concepts
Victer Paul
 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
ankurgupta857016
 
Lecture 6
Lecture 6Lecture 6
Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"Gouda Mando
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
MURADSANJOUM
 
Lecture 7 arrays
Lecture   7 arraysLecture   7 arrays
Lecture 7 arrays
manish kumar
 
DSA 103 Object Oriented Programming :: Week 5
DSA 103 Object Oriented Programming :: Week 5DSA 103 Object Oriented Programming :: Week 5
DSA 103 Object Oriented Programming :: Week 5
Ferdin Joe John Joseph PhD
 
Pi j3.4 data-structures
Pi j3.4 data-structuresPi j3.4 data-structures
Pi j3.4 data-structures
mcollison
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arraysAseelhalees
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
Shahzaib Ajmal
 
Java arrays
Java    arraysJava    arrays
Java arrays
Mohammed Sikander
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injavairdginfo
 
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
SatyajeetGaur3
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
shafat6712
 
JavaProgramming 17321_CS202-Chapter8.ppt
JavaProgramming 17321_CS202-Chapter8.pptJavaProgramming 17321_CS202-Chapter8.ppt
JavaProgramming 17321_CS202-Chapter8.ppt
MohammedNouh7
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)Ravi_Kant_Sahu
 
L10 array
L10 arrayL10 array
L10 array
teach4uin
 

Similar to Array (20)

Java - Arrays Concepts
Java - Arrays ConceptsJava - Arrays Concepts
Java - Arrays Concepts
 
07slide.ppt
07slide.ppt07slide.ppt
07slide.ppt
 
Lecture 6
Lecture 6Lecture 6
Lecture 6
 
Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"Java™ (OOP) - Chapter 6: "Arrays"
Java™ (OOP) - Chapter 6: "Arrays"
 
06slide
06slide06slide
06slide
 
Array
ArrayArray
Array
 
07+08slide.pptx
07+08slide.pptx07+08slide.pptx
07+08slide.pptx
 
Lecture 7 arrays
Lecture   7 arraysLecture   7 arrays
Lecture 7 arrays
 
DSA 103 Object Oriented Programming :: Week 5
DSA 103 Object Oriented Programming :: Week 5DSA 103 Object Oriented Programming :: Week 5
DSA 103 Object Oriented Programming :: Week 5
 
Pi j3.4 data-structures
Pi j3.4 data-structuresPi j3.4 data-structures
Pi j3.4 data-structures
 
Multi dimensional arrays
Multi dimensional arraysMulti dimensional arrays
Multi dimensional arrays
 
Java arrays
Java arraysJava arrays
Java arrays
 
C Language Lecture 10
C Language Lecture 10C Language Lecture 10
C Language Lecture 10
 
Java arrays
Java    arraysJava    arrays
Java arrays
 
6 arrays injava
6 arrays injava6 arrays injava
6 arrays injava
 
CAP615-Unit1.pptx
CAP615-Unit1.pptxCAP615-Unit1.pptx
CAP615-Unit1.pptx
 
6_Array.pptx
6_Array.pptx6_Array.pptx
6_Array.pptx
 
JavaProgramming 17321_CS202-Chapter8.ppt
JavaProgramming 17321_CS202-Chapter8.pptJavaProgramming 17321_CS202-Chapter8.ppt
JavaProgramming 17321_CS202-Chapter8.ppt
 
String handling(string class)
String handling(string class)String handling(string class)
String handling(string class)
 
L10 array
L10 arrayL10 array
L10 array
 

More from Ravi_Kant_Sahu

Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in Java
Ravi_Kant_Sahu
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java Ravi_Kant_Sahu
 

More from Ravi_Kant_Sahu (7)

Common Programming Errors by Beginners in Java
Common Programming Errors by Beginners in JavaCommon Programming Errors by Beginners in Java
Common Programming Errors by Beginners in Java
 
Event handling
Event handlingEvent handling
Event handling
 
List classes
List classesList classes
List classes
 
Collection framework
Collection frameworkCollection framework
Collection framework
 
Jdbc
JdbcJdbc
Jdbc
 
Swing api
Swing apiSwing api
Swing api
 
Genesis and Overview of Java
Genesis and Overview of Java Genesis and Overview of Java
Genesis and Overview of Java
 

Recently uploaded

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
Safe Software
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
Alison B. Lowndes
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
Kari Kakkonen
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
Product School
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
91mobiles
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
DianaGray10
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
Product School
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
Fwdays
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
Abida Shariff
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
Ralf Eggert
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
Alan Dix
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
Elena Simperl
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
Guy Korland
 

Recently uploaded (20)

Essentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with ParametersEssentials of Automations: Optimizing FME Workflows with Parameters
Essentials of Automations: Optimizing FME Workflows with Parameters
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
DevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA ConnectDevOps and Testing slides at DASA Connect
DevOps and Testing slides at DASA Connect
 
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
De-mystifying Zero to One: Design Informed Techniques for Greenfield Innovati...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdfSmart TV Buyer Insights Survey 2024 by 91mobiles.pdf
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3UiPath Test Automation using UiPath Test Suite series, part 3
UiPath Test Automation using UiPath Test Suite series, part 3
 
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
From Daily Decisions to Bottom Line: Connecting Product Work to Revenue by VP...
 
"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi"Impact of front-end architecture on development cost", Viktor Turskyi
"Impact of front-end architecture on development cost", Viktor Turskyi
 
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptxIOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
IOS-PENTESTING-BEGINNERS-PRACTICAL-GUIDE-.pptx
 
PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)PHP Frameworks: I want to break free (IPC Berlin 2024)
PHP Frameworks: I want to break free (IPC Berlin 2024)
 
Epistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI supportEpistemic Interaction - tuning interfaces to provide information for AI support
Epistemic Interaction - tuning interfaces to provide information for AI support
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Knowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and backKnowledge engineering: from people to machines and back
Knowledge engineering: from people to machines and back
 
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdfFIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
FIDO Alliance Osaka Seminar: FIDO Security Aspects.pdf
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
GraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge GraphGraphRAG is All You need? LLM & Knowledge Graph
GraphRAG is All You need? LLM & Knowledge Graph
 

Array

  • 1. Programming in Java Lecture 10: Arrays By Ravi Kant Sahu Asst. Professor, LPU
  • 2. Array • Definition: An array is a group/collection of variables of the same type that are referred to by a common name. • Arrays of any type can be created and may have one or more dimensions. • A specific element in an array is accessed by its index (subscript). • Examples: • Collection of numbers • Collection of names • Collection of suffixes Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 3. Array of numbers: Array of names: Array of suffixes: Examples 10 23 863 8 229 ment tion ness ves Sam Shanu Riya Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 4. One-Dimensional Arrays • A one-dimensional array is a list of variables of same type. • The general form of a one-dimensional array declaration is: type var-name[ ]; array-var = new type[size]; or, type var-name[ ] = new type[size]; Example: int num [] = new int [10]; Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 5. Declaration of array variable: data-type variable-name[]; eg. int marks[]; This will declare an array named ‘marks’ of type ‘int’. But no memory is allocated to the array. Allocation of memory: variable-name = new data-type[size]; eg. marks = new int[5]; This will allocate memory of 5 integers to the array ‘marks’ and it can store upto 5 integers in it. ‘new’ is a special operator that allocates memory. Syntax Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 6. Accessing elements in the array: • Specific element in the array is accessed by specifying name of the array followed the index of the element. • All array indexes in Java start at zero. variable-name[index] = value; Example: marks[0] = 10; This will assign the value 10 to the 1st element in the array. marks[2] = 863;; This will assign the value 863 to the 3rd element in the array. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 7. STEP 1 : (Declaration) int marks[]; marks  null STEP 2: (Memory Allocation) marks = new int[5]; marks  marks[0] marks[1] marks[2] marks[3] marks[4] STEP 3: (Accessing Elements) marks[0] = 10; marks  marks[0] marks[1] marks[2] marks[3] marks[4] Example 0 0 0 0 0 10 0 0 0 0 Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 8. • Size of an array can’t be changed after the array is created. • Default values: – zero (0) for numeric data types, – u0000 for chars and – false for boolean types • Length of an array can be obtained as: array_ref_var.length Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 9. Example class Demo_Array { public static void main(String args[]) { int marks[]; marks = new int[3]; marks[0] = 10; marks[1] = 35; marks[2] = 84; System.out.println(“Marks of 2nd student=” + marks[1]); } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 10. • Arrays can store elements of the same data type. Hence an int array CAN NOT store an element which is not an int. • Though an element of a compatible type can be converted to int and stored into the int array. eg. marks[2] = (int) 22.5; This will convert ‘22.5’ into the int part ‘22’ and store it into the 3rd place in the int array ‘marks’. • Array indexes start from zero. Hence ‘marks[index]’ refers to the (index+1)th element in the array and ‘marks[size-1]’ refers to last element in the array. Note Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 11. Array Initialization 1. data Type [] array_ref_var = {value0, value1, …, value n}; 2. data Type [] array_ref_var; array_ref_var = {value0, value1, …,value n}; 3. data Type [] array_ref_var = new data Type [n+1]; array_ref_var [0] = value 0; array_ref_var [1] = value 1; … array_ref_var [n] = value n; Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 12. Example int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); v 00 0 00 0 00 00 v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8] Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 13. Example int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); v 01 0 00 0 00 00 v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]
  • 14. Example int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); v 01 0 00 0 50 00 v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]
  • 15. Example int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); v 81 0 00 0 50 00 v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]
  • 16. Example int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); v 81 0 06 0 50 00 v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]
  • 17. Example int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); v 81 0 06 0 50 012 v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8]
  • 18. Example int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); v 81 0 06 0 50 012 v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8] 8 is displayed
  • 19. Example int[] v = new int[10]; int i = 7; int j = 2; int k = 4; v[0] = 1; v[i] = 5; v[j] = v[i] + 3; v[j+1] = v[i] + v[0]; v[v[j]] = 12; System.out.println(v[2]); v[k] = stdin.nextInt(); v 81 0 06 3 50 012 v[2]v[0] v[1] v[5]v[3] v[4] v[7]v[6] v[9]v[8] Suppose 3 is extracted
  • 20. Multi-Dimensional Array • Multidimensional arrays are arrays of arrays. • Two-Dimensional arrays are used to represent a table or a matrix. • Creating Two-Dimensional Array: int twoD[][] = new int[4][5]; Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 21. Conceptual View of 2-Dimensional Array Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 22. class TwoDimArr { public static void main(String args[]) { int twoD[][]= new int[4][5]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<5; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<5; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 23. • When we allocate memory for a multidimensional array, we need to only specify the memory for the first (leftmost) dimension. int twoD[][] = new int[4][]; • The other dimensions can be assigned manually. Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 24. // Manually allocate differing size second dimensions. class TwoDAgain { public static void main(String args[]) { int twoD[][] = new int[4][]; twoD[0] = new int[1]; twoD[1] = new int[2]; twoD[2] = new int[3]; twoD[3] = new int[4]; int i, j, k = 0; for(i=0; i<4; i++) for(j=0; j<i+1; j++) { twoD[i][j] = k; k++; } for(i=0; i<4; i++) { for(j=0; j<i+1; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } }Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 25. Initializing Multi-Dimensional Array class Matrix { public static void main(String args[]) { double m[][] = { { 0*0, 1*0, 2*0, 3*0 }, { 0*1, 1*1, 2*1, 3*1 }, { 0*2, 1*2, 2*2, 3*2 }, { 0*3, 1*3, 2*3, 3*3 } }; int i, j; for(i=0; i<4; i++) { for(j=0; j<4; j++) System.out.print(m[i][j] + " "); System.out.println(); } } } Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 26. Alternative Array Declaration type[ ] var-name; • Example: char twod1[][] = new char[3][4]; char[][] twod2 = new char[3][4]; • This alternative declaration form offers convenience when declaring several arrays at the same time. • Example: int[] nums, nums2, nums3; Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 27. Array Cloning • To actually create another array with its own values, Java provides the clone()method. • arr2 = arr1; (assignment) is not equivalent to arr2 = arr1.clone(); (cloning) Example: ArrayCloning.java Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 28. Array as Argument • Arrays can be passed as an argument to any method. Example: void show ( int []) {} public static void main(String arr[]) {} void compareArray(int [] a, int [] b ){} Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)
  • 29. Array as a return Type • Arrays can also be returned as the return type of any method Example: public int [] Result (int roll_No, int marks ) Ravi Kant Sahu, Asst. Professor @ Lovely Professional University, Punjab (India)