SlideShare a Scribd company logo
© Amir Kirsh
PROGRAMMING IN JAVA
ARRAYS
This Keyword
The this keyword refers to the current object in a method or
constructor.
The most common use of this keyword is to eliminate the confusion
between class attributes and parameters with the same name.
This Keyword
class Student
{
int regNo;
String name;
Student (int regNo, String name)
{
this.regNo = regNo;
this.name = name;
}
}
No Problem Assignment : instance Vs formal args
class Student
{
int regNo;
String name;
Student (int r, String n)
{
regNo = r;
name = n;
}
}
Without This Keyword
class StudentNoThis
{
int regNo;
String name;
StudentNoThis (int regNo, String name)
{
regNo = regNo;
name = name;
name = name;
}
void display ()
{
System.out.println ("Student Roll Number is: " + regNo);
System.out.println ("Student Name is: " + name);
}
}
This Keyword
class WithoutThis
{
public static void main(String args[])
{
StudentNoThis s2 = new StudentNoThis (102, "Mohamed");
System.out.println ("s2 object contains:");
System.out.println ("s2 object contains:");
s2.display ();
}
}
Output
s2 object contains:
Student Roll Number is: 0
Student Name is: null
Array
 An Array is a collection of elements that share the same type and
name. The elements from the array can be accessed by the index.
 For example, an array as ‘marks’ can be defined to represent a
set of marks of a group of students
 A specific element in an array is accessed by the use of a
subscript or an index used inside the brackets, along with the
subscript or an index used inside the brackets, along with the
name of the array.
 For example, marks[5] would store the marks of the fifth student.
While the complete set of values is called an array, the individual
values are known as elements
Array
Arrays can be two types:
one dimensional array
multi-dimensional array
One Dimensional Array
 In a one-dimensional array, a single subscript or index is used,
where each index value refers to an individual array element.
 The indexation will start from 0 and will go up to n –1, i.e., the
first value of the array will have an index of 0 and the last value
will have an index of n –1, where n is the number of elements in
the array.
One Dimensional Array
 For example, if an array named marks has been declared to
store the marks of five students, the computer reserves five
contiguous locations in the memory.
 The five marks to be assigned to each array element are 60, 58,
50, 78, and 89.
 It will be done as follows:
 It will be done as follows:
Marks[0] = 60;
Marks[1] = 58;
Marks[2] = 50;
Marks[3] = 78;
Marks[4] = 89;
One Dimensional Array
One Dimensional Array
Creation of Array
Creating an array, similar to an object creation, can inherently
involve three steps:
 Declaring an array
 Creating memory locations
 Creating memory locations
 Initializing/assigning values to an array
Declaring an Array
Declaring an array is same as declaring a normal variable except
that it must use a set of square brackets with the variable type.
There can be two ways in which an array can be declared.
 type arrayname[];
 type arrayname[];
 type[] arrayname;
So the above marks array having elements of integer type can be
declared either as
int marks[]; or int[] marks;
Creating Memory Locations
 An array can be assigned to memory when it is declared it and
also the size can be specified.
Syntax: Arrayname = new type [size];
Example: marks = new int[5];
 Both (declaration of array and creation of memory location), help
in the creation of an array.
 Syntax:
type arrayname[] = new type[]; or type[] arrayname = new type[];
Example: int marks[] = new int[5];
Initializing/ assigning Values to an Array
Assignment of values to an array, which can also be termed as
initialization of array, can be done as follows:
Syntax: Arrayname[index] = value;
Example:
int[] marks = new int[5];
int[] marks = new int[5];
marks[0] = 60;
marks[1] = 58;
marks[2] = 50;
marks[3] = 78;
marks[4] = 89;
Initializing/ assigning Values to an Array
Arrays can alternately be assigned values or initialized in the same
way as the variables, i.e., at the time of declaration itself.
Syntax:
type arrayname[] = {list of values};
Example:
Example:
int marks[] = {60, 58, 50, 78, 89}
for Loops with Arrays
The for loops can be used to assign as well as access values from
an array. To obtain the number of values in an array, i.e., the length
of the array, we use the name of the array followed by the dot
operator and the variable length. This length property is associated
with all the arrays in Java.
with all the arrays in Java.
int[] marks = {60, 58, 50, 78, 89};
for (int i = 0; i<marks.length; i++)
{
System.out.println(marks[i]);
}
for Loops with Arrays
To Print Array Values
for (int i = 0; i<marks.length; i++)
System.out.println(marks[i]);
for Loops with Arrays
import java.util.*;
class ArrayTest
{
public static void main(String args[]) {
Scanner dis=new Scanner(System.in);
int a[],n,i;
System.out.println("Enter the size of Array:");
n=dis.nextInt();
n=dis.nextInt();
a=new int[n];
System.out.println("Enter the elements into Array:");
for(i=0;i<n;i++) {
a[i]=dis.nextInt();
}
System.out.println("The elements of Array:");
for(i=0;i<n;i++) {
System.out.print(a[i]+" ");
}
}}
Two dimensional Arrays
Sometimes values can be conceptualized in the form of a table that
is in the form of rows and columns.
Syntax: datatype arr-name[row-size][column-size];
Example: int a[3][3];
The elements are stored in memory locations as follows:
The elements are stored in memory locations as follows:
Column0 Column 1 Column 2
Row 0 a[0][0] a[0][1] a[0][2]
Row 1 a[1][0] a[1][1] a[1][2]
Row 2 a[2][0] a[2][1] a[2][2]
Two dimensional Arrays
Two dimensional Arrays
Two dimensional Arrays
Like a one-dimensional array, two-dimensional arrays may be
initialized with values at the time of their creation. For example,
int marks[2][4] = {2, 3, 6, 0, 9, 3, 3, 2};
This declaration shows that the first two rows of a 2 × 4 matrix have
been initialized by the values shown in the list above
been initialized by the values shown in the list above
Ad

More Related Content

Similar to Arrays a detailed explanation and presentation (20)

Arrays Basicfundamentaldatastructure.ppt
Arrays Basicfundamentaldatastructure.pptArrays Basicfundamentaldatastructure.ppt
Arrays Basicfundamentaldatastructure.ppt
JyothiAmpally
 
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
ArraysArrays
Arrays
Neeru Mittal
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
soniya555961
 
Arrays In C
Arrays In CArrays In C
Arrays In C
yndaravind
 
Lecture 15 Arrays with C++ programming.ppt
Lecture 15  Arrays with C++ programming.pptLecture 15  Arrays with C++ programming.ppt
Lecture 15 Arrays with C++ programming.ppt
SamahAdel16
 
Ch08
Ch08Ch08
Ch08
Arriz San Juan
 
Java R20 - UNIT-3.docx
Java R20 - UNIT-3.docxJava R20 - UNIT-3.docx
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
Introduction to Arrays in C
Introduction to Arrays in CIntroduction to Arrays in C
Introduction to Arrays in C
Thesis Scientist Private Limited
 
Array in C
Array in CArray in C
Array in C
adityas29
 
ARRAYS in java with in details presentation.ppt
ARRAYS in java with in details presentation.pptARRAYS in java with in details presentation.ppt
ARRAYS in java with in details presentation.ppt
AshokRachapalli1
 
Array and 2D Array and with syntax working
Array and 2D Array and with syntax workingArray and 2D Array and with syntax working
Array and 2D Array and with syntax working
shahrukhkamal7
 
Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...
ssuser6478a8
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
ssuser99ca78
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
coding9
 
Arrays
ArraysArrays
Arrays
Steven Wallach
 
DOC-20240812-WA0000 array string and.pptx
DOC-20240812-WA0000 array string and.pptxDOC-20240812-WA0000 array string and.pptx
DOC-20240812-WA0000 array string and.pptx
PanjatcharamVg
 
05slide_arrays_creation_searching_sorting.ppt
05slide_arrays_creation_searching_sorting.ppt05slide_arrays_creation_searching_sorting.ppt
05slide_arrays_creation_searching_sorting.ppt
ramesh504657
 
ARRAYSinJavaProgramming_Introduction.ppt
ARRAYSinJavaProgramming_Introduction.pptARRAYSinJavaProgramming_Introduction.ppt
ARRAYSinJavaProgramming_Introduction.ppt
cluttertans
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
worldchannel
 
Arrays Basicfundamentaldatastructure.ppt
Arrays Basicfundamentaldatastructure.pptArrays Basicfundamentaldatastructure.ppt
Arrays Basicfundamentaldatastructure.ppt
JyothiAmpally
 
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
 
Lecture 15 Arrays with C++ programming.ppt
Lecture 15  Arrays with C++ programming.pptLecture 15  Arrays with C++ programming.ppt
Lecture 15 Arrays with C++ programming.ppt
SamahAdel16
 
Java R20 - UNIT-3.docx
Java R20 - UNIT-3.docxJava R20 - UNIT-3.docx
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
ARRAYS in java with in details presentation.ppt
ARRAYS in java with in details presentation.pptARRAYS in java with in details presentation.ppt
ARRAYS in java with in details presentation.ppt
AshokRachapalli1
 
Array and 2D Array and with syntax working
Array and 2D Array and with syntax workingArray and 2D Array and with syntax working
Array and 2D Array and with syntax working
shahrukhkamal7
 
Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...Arrays are used to store multiple values in a single variable, instead of dec...
Arrays are used to store multiple values in a single variable, instead of dec...
ssuser6478a8
 
ARRAYS.ppt
ARRAYS.pptARRAYS.ppt
ARRAYS.ppt
coding9
 
DOC-20240812-WA0000 array string and.pptx
DOC-20240812-WA0000 array string and.pptxDOC-20240812-WA0000 array string and.pptx
DOC-20240812-WA0000 array string and.pptx
PanjatcharamVg
 
05slide_arrays_creation_searching_sorting.ppt
05slide_arrays_creation_searching_sorting.ppt05slide_arrays_creation_searching_sorting.ppt
05slide_arrays_creation_searching_sorting.ppt
ramesh504657
 
ARRAYSinJavaProgramming_Introduction.ppt
ARRAYSinJavaProgramming_Introduction.pptARRAYSinJavaProgramming_Introduction.ppt
ARRAYSinJavaProgramming_Introduction.ppt
cluttertans
 
Intro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technologyIntro to C# - part 2.pptx emerging technology
Intro to C# - part 2.pptx emerging technology
worldchannel
 

More from riazahamed37 (8)

Functions in Python with all type of arguments
Functions in Python with all type of argumentsFunctions in Python with all type of arguments
Functions in Python with all type of arguments
riazahamed37
 
Python Way of Program is a topic for beginners
Python Way of Program is a topic for beginnersPython Way of Program is a topic for beginners
Python Way of Program is a topic for beginners
riazahamed37
 
JTABBED PANE is a swing concept used to have tabs
JTABBED PANE is a swing concept used to have tabsJTABBED PANE is a swing concept used to have tabs
JTABBED PANE is a swing concept used to have tabs
riazahamed37
 
JCheckBox is a light weight component of java
JCheckBox is a light weight component of javaJCheckBox is a light weight component of java
JCheckBox is a light weight component of java
riazahamed37
 
This presentation is about swing concept in python
This presentation is about swing concept in pythonThis presentation is about swing concept in python
This presentation is about swing concept in python
riazahamed37
 
JTRee is a swing concept and it is said to be light weight component
JTRee is a swing concept and it is said to be light weight componentJTRee is a swing concept and it is said to be light weight component
JTRee is a swing concept and it is said to be light weight component
riazahamed37
 
CONTROL FLOW STRUCTURE IN JAVA LANG.pdf
CONTROL FLOW  STRUCTURE IN JAVA LANG.pdfCONTROL FLOW  STRUCTURE IN JAVA LANG.pdf
CONTROL FLOW STRUCTURE IN JAVA LANG.pdf
riazahamed37
 
object oriented programming in javaTERNARY OPERATORS.pptx
object oriented programming in javaTERNARY OPERATORS.pptxobject oriented programming in javaTERNARY OPERATORS.pptx
object oriented programming in javaTERNARY OPERATORS.pptx
riazahamed37
 
Functions in Python with all type of arguments
Functions in Python with all type of argumentsFunctions in Python with all type of arguments
Functions in Python with all type of arguments
riazahamed37
 
Python Way of Program is a topic for beginners
Python Way of Program is a topic for beginnersPython Way of Program is a topic for beginners
Python Way of Program is a topic for beginners
riazahamed37
 
JTABBED PANE is a swing concept used to have tabs
JTABBED PANE is a swing concept used to have tabsJTABBED PANE is a swing concept used to have tabs
JTABBED PANE is a swing concept used to have tabs
riazahamed37
 
JCheckBox is a light weight component of java
JCheckBox is a light weight component of javaJCheckBox is a light weight component of java
JCheckBox is a light weight component of java
riazahamed37
 
This presentation is about swing concept in python
This presentation is about swing concept in pythonThis presentation is about swing concept in python
This presentation is about swing concept in python
riazahamed37
 
JTRee is a swing concept and it is said to be light weight component
JTRee is a swing concept and it is said to be light weight componentJTRee is a swing concept and it is said to be light weight component
JTRee is a swing concept and it is said to be light weight component
riazahamed37
 
CONTROL FLOW STRUCTURE IN JAVA LANG.pdf
CONTROL FLOW  STRUCTURE IN JAVA LANG.pdfCONTROL FLOW  STRUCTURE IN JAVA LANG.pdf
CONTROL FLOW STRUCTURE IN JAVA LANG.pdf
riazahamed37
 
object oriented programming in javaTERNARY OPERATORS.pptx
object oriented programming in javaTERNARY OPERATORS.pptxobject oriented programming in javaTERNARY OPERATORS.pptx
object oriented programming in javaTERNARY OPERATORS.pptx
riazahamed37
 
Ad

Recently uploaded (20)

Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptxIncreasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Increasing Retail Store Efficiency How can Planograms Save Time and Money.pptx
Anoop Ashok
 
TrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business ConsultingTrsLabs - Fintech Product & Business Consulting
TrsLabs - Fintech Product & Business Consulting
Trs Labs
 
Cybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure ADCybersecurity Identity and Access Solutions using Azure AD
Cybersecurity Identity and Access Solutions using Azure AD
VICTOR MAESTRE RAMIREZ
 
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath MaestroDev Dives: Automate and orchestrate your processes with UiPath Maestro
Dev Dives: Automate and orchestrate your processes with UiPath Maestro
UiPathCommunity
 
Big Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur MorganBig Data Analytics Quick Research Guide by Arthur Morgan
Big Data Analytics Quick Research Guide by Arthur Morgan
Arthur Morgan
 
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager APIUiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPath Community Berlin: Orchestrator API, Swagger, and Test Manager API
UiPathCommunity
 
Role of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered ManufacturingRole of Data Annotation Services in AI-Powered Manufacturing
Role of Data Annotation Services in AI-Powered Manufacturing
Andrew Leo
 
Heap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and DeletionHeap, Types of Heap, Insertion and Deletion
Heap, Types of Heap, Insertion and Deletion
Jaydeep Kale
 
Semantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AISemantic Cultivators : The Critical Future Role to Enable AI
Semantic Cultivators : The Critical Future Role to Enable AI
artmondano
 
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdfSAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
SAP Modernization: Maximizing the Value of Your SAP S/4HANA Migration.pdf
Precisely
 
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
#StandardsGoals for 2025: Standards & certification roundup - Tech Forum 2025
BookNet Canada
 
Mobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi ArabiaMobile App Development Company in Saudi Arabia
Mobile App Development Company in Saudi Arabia
Steve Jonas
 
HCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser EnvironmentsHCL Nomad Web – Best Practices and Managing Multiuser Environments
HCL Nomad Web – Best Practices and Managing Multiuser Environments
panagenda
 
Build Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For DevsBuild Your Own Copilot & Agents For Devs
Build Your Own Copilot & Agents For Devs
Brian McKeiver
 
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc Webinar: Consumer Expectations vs Corporate Realities on Data Broker...
TrustArc
 
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
AI Changes Everything – Talk at Cardiff Metropolitan University, 29th April 2...
Alan Dix
 
Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.Greenhouse_Monitoring_Presentation.pptx.
Greenhouse_Monitoring_Presentation.pptx.
hpbmnnxrvb
 
How analogue intelligence complements AI
How analogue intelligence complements AIHow analogue intelligence complements AI
How analogue intelligence complements AI
Paul Rowe
 
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptxSpecial Meetup Edition - TDX Bengaluru Meetup #52.pptx
Special Meetup Edition - TDX Bengaluru Meetup #52.pptx
shyamraj55
 
2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx2025-05-Q4-2024-Investor-Presentation.pptx
2025-05-Q4-2024-Investor-Presentation.pptx
Samuele Fogagnolo
 
Ad

Arrays a detailed explanation and presentation

  • 1. © Amir Kirsh PROGRAMMING IN JAVA ARRAYS
  • 2. This Keyword The this keyword refers to the current object in a method or constructor. The most common use of this keyword is to eliminate the confusion between class attributes and parameters with the same name.
  • 3. This Keyword class Student { int regNo; String name; Student (int regNo, String name) { this.regNo = regNo; this.name = name; } }
  • 4. No Problem Assignment : instance Vs formal args class Student { int regNo; String name; Student (int r, String n) { regNo = r; name = n; } }
  • 5. Without This Keyword class StudentNoThis { int regNo; String name; StudentNoThis (int regNo, String name) { regNo = regNo; name = name; name = name; } void display () { System.out.println ("Student Roll Number is: " + regNo); System.out.println ("Student Name is: " + name); } }
  • 6. This Keyword class WithoutThis { public static void main(String args[]) { StudentNoThis s2 = new StudentNoThis (102, "Mohamed"); System.out.println ("s2 object contains:"); System.out.println ("s2 object contains:"); s2.display (); } } Output s2 object contains: Student Roll Number is: 0 Student Name is: null
  • 7. Array  An Array is a collection of elements that share the same type and name. The elements from the array can be accessed by the index.  For example, an array as ‘marks’ can be defined to represent a set of marks of a group of students  A specific element in an array is accessed by the use of a subscript or an index used inside the brackets, along with the subscript or an index used inside the brackets, along with the name of the array.  For example, marks[5] would store the marks of the fifth student. While the complete set of values is called an array, the individual values are known as elements
  • 8. Array Arrays can be two types: one dimensional array multi-dimensional array
  • 9. One Dimensional Array  In a one-dimensional array, a single subscript or index is used, where each index value refers to an individual array element.  The indexation will start from 0 and will go up to n –1, i.e., the first value of the array will have an index of 0 and the last value will have an index of n –1, where n is the number of elements in the array.
  • 10. One Dimensional Array  For example, if an array named marks has been declared to store the marks of five students, the computer reserves five contiguous locations in the memory.  The five marks to be assigned to each array element are 60, 58, 50, 78, and 89.  It will be done as follows:  It will be done as follows: Marks[0] = 60; Marks[1] = 58; Marks[2] = 50; Marks[3] = 78; Marks[4] = 89;
  • 12. One Dimensional Array Creation of Array Creating an array, similar to an object creation, can inherently involve three steps:  Declaring an array  Creating memory locations  Creating memory locations  Initializing/assigning values to an array
  • 13. Declaring an Array Declaring an array is same as declaring a normal variable except that it must use a set of square brackets with the variable type. There can be two ways in which an array can be declared.  type arrayname[];  type arrayname[];  type[] arrayname; So the above marks array having elements of integer type can be declared either as int marks[]; or int[] marks;
  • 14. Creating Memory Locations  An array can be assigned to memory when it is declared it and also the size can be specified. Syntax: Arrayname = new type [size]; Example: marks = new int[5];  Both (declaration of array and creation of memory location), help in the creation of an array.  Syntax: type arrayname[] = new type[]; or type[] arrayname = new type[]; Example: int marks[] = new int[5];
  • 15. Initializing/ assigning Values to an Array Assignment of values to an array, which can also be termed as initialization of array, can be done as follows: Syntax: Arrayname[index] = value; Example: int[] marks = new int[5]; int[] marks = new int[5]; marks[0] = 60; marks[1] = 58; marks[2] = 50; marks[3] = 78; marks[4] = 89;
  • 16. Initializing/ assigning Values to an Array Arrays can alternately be assigned values or initialized in the same way as the variables, i.e., at the time of declaration itself. Syntax: type arrayname[] = {list of values}; Example: Example: int marks[] = {60, 58, 50, 78, 89}
  • 17. for Loops with Arrays The for loops can be used to assign as well as access values from an array. To obtain the number of values in an array, i.e., the length of the array, we use the name of the array followed by the dot operator and the variable length. This length property is associated with all the arrays in Java. with all the arrays in Java. int[] marks = {60, 58, 50, 78, 89}; for (int i = 0; i<marks.length; i++) { System.out.println(marks[i]); }
  • 18. for Loops with Arrays To Print Array Values for (int i = 0; i<marks.length; i++) System.out.println(marks[i]);
  • 19. for Loops with Arrays import java.util.*; class ArrayTest { public static void main(String args[]) { Scanner dis=new Scanner(System.in); int a[],n,i; System.out.println("Enter the size of Array:"); n=dis.nextInt(); n=dis.nextInt(); a=new int[n]; System.out.println("Enter the elements into Array:"); for(i=0;i<n;i++) { a[i]=dis.nextInt(); } System.out.println("The elements of Array:"); for(i=0;i<n;i++) { System.out.print(a[i]+" "); } }}
  • 20. Two dimensional Arrays Sometimes values can be conceptualized in the form of a table that is in the form of rows and columns. Syntax: datatype arr-name[row-size][column-size]; Example: int a[3][3]; The elements are stored in memory locations as follows: The elements are stored in memory locations as follows: Column0 Column 1 Column 2 Row 0 a[0][0] a[0][1] a[0][2] Row 1 a[1][0] a[1][1] a[1][2] Row 2 a[2][0] a[2][1] a[2][2]
  • 23. Two dimensional Arrays Like a one-dimensional array, two-dimensional arrays may be initialized with values at the time of their creation. For example, int marks[2][4] = {2, 3, 6, 0, 9, 3, 3, 2}; This declaration shows that the first two rows of a 2 × 4 matrix have been initialized by the values shown in the list above been initialized by the values shown in the list above