SlideShare a Scribd company logo
Data Structure Lecture 2
Following operations can be performed on the data structures:
1. Traversing
2. Searching
3. Inserting
4. Deleting
5. Sorting
6. Merging
1. Traversing- It is used to access each data item exactly once so
that it can be processed.
2. Searching- It is used to find out the location of the data item if it
exists in the given collection of data items.
3. Inserting- It is used to add a new data item in the given
collection of data items.
4. Deleting- It is used to delete an existing data item from the given
collection of data items.
5. Sorting- It is used to arrange the data items in some order i.e. in
ascending or descending order in case of numerical data and in
dictionary order in case of alphanumeric data.
6. Merging- It is used to combine the data items of two sorted files
into single file in the sorted form.
1
Array Data Structure
Introduction
An array is an aggregate data structure that is designed to store a
group of objects of the same or different types. Arrays can hold
primitives as well as references. The array is the most efficient data
structure for storing and accessing a sequence of objects.
Here is the list of most important array features you must know
(i.e. be able to program)
• copying and cloning
• insertion and deletion
• searching and sorting
You already know that the Java language has only two data types,
primitives and references. Which one is an array? Is it primitive?
An array is not a primitive data type - it has a field (and only one),
called length. Formally speaking, an array is a reference type,
though you cannot find such a class in the Java APIs. Therefore,
you deal with arrays as you deal with references. One of the major
diffeences between refeences and primituives is that you cannot
copy arrays by assigning one to another:
int[] a = {9, 5, 4};
int[] b = a;
The assignment operator creates an alias to the object, like in the
picture below
2
Since these two references a and b refer to the same object,
comparing them with the double equal sign "==" will always return
true. In the next code example,
int [] a = {1,2,3};
int [] b = {1,2,3};
a and b refer to two different objects (though with identical
contents). Comparing them with the double equal sign will return
false. How would you compare two objects with identical
contents? In short, using the equals method. For array
comparison, the Java APIs provides the Arrays class.
The Arrays class
The java.util.Arrays class is a convenience class for various array
manipulations, like comparison, searching, printing, sorting and
others. Basically, this class is a set of static methods that are all
useful for working with arrays. The code below demonstrates a
proper invocation ofequals:
int[] a = {1,2,3};
int[] b = {1,2,3};
if( Arrays.equals(a, b) )
System.out.println("arrays with
identical contents");
3
Another commonly used method is toString() which takes
care of of printing
int[] a = {1,2,3};
System.out.println(Arrays.toString(a));
Here is the example of sorting
int[] a = {3,2,1};
Arrays.sort(a);
System.out.println(Arrays.toString(a));
In addition to that, the class has other utility methods for
supporting operations over multidimensional arrays.
Copying arrays
There are four ways to copy arrays
1. using a loop structure
2. using Arrays.copyOf()
3. using System.arraycopy()
4. using clone()
The first way is very well known to you
int[] a = {1, 2, 3};
int[] b = new int[a.length];
for(int i = 0; i ‹ a.length; i++) b[i] =
a[i];
The next choice is to use Arrays.copyOf()
int[] a = {1, 2, 3};
int[] b = Arrays.copyOf(a, a.length);
4
The second parameter specifies the length of the new array, which
could either less or equal or bigger than the original length.
The most efficient copying data between arrays is provided
by System.arraycopy() method. The method requires five arguments.
Here is its signature
public static void arraycopy(Object source,
int srcIndex,
Object
destination,
int destIndex,
int length)
The method copies length elements from a source array
starting with the index srcIndex to a new
array destination at the indexdestIndex.The above code
example can be rewritten as it follows
int[] a = {1, 2, 3};
int[] b = new int[a.length];
System.arraycopy(a, 0, b, 0, 3)
And the last copying choice is the use of cloning. Cloning involves
creating a new array of the same size and type and copying all the
old elements into the new array. The clone() method is defined
in the Object class and its invocation is demonstrated by this
code segment
int[] a = {1, 2, 3};
int[] b = (int[]) a.clone();
5
The second parameter specifies the length of the new array, which
could either less or equal or bigger than the original length.
The most efficient copying data between arrays is provided
by System.arraycopy() method. The method requires five arguments.
Here is its signature
public static void arraycopy(Object source,
int srcIndex,
Object
destination,
int destIndex,
int length)
The method copies length elements from a source array
starting with the index srcIndex to a new
array destination at the indexdestIndex.The above code
example can be rewritten as it follows
int[] a = {1, 2, 3};
int[] b = new int[a.length];
System.arraycopy(a, 0, b, 0, 3)
And the last copying choice is the use of cloning. Cloning involves
creating a new array of the same size and type and copying all the
old elements into the new array. The clone() method is defined
in the Object class and its invocation is demonstrated by this
code segment
int[] a = {1, 2, 3};
int[] b = (int[]) a.clone();
5

More Related Content

What's hot

Data structure and its types
Data structure and its typesData structure and its types
Data structure and its typesNavtar Sidhu Brar
 
Data structures
Data structuresData structures
Data Structures Notes 2021
Data Structures Notes 2021Data Structures Notes 2021
Data Structures Notes 2021
Sreedhar Chowdam
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structureeShikshak
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
Rai University
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
Nikhil Pandit
 
Data structure & algorithms introduction
Data structure & algorithms introductionData structure & algorithms introduction
Data structure & algorithms introduction
Sugandh Wafai
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
Rai University
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
Rai University
 
Introduction to Data Structure
Introduction to Data Structure Introduction to Data Structure
Introduction to Data Structure
Prof Ansari
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1Kumar
 
Ii pu cs practical viva voce questions
Ii pu cs  practical viva voce questionsIi pu cs  practical viva voce questions
Ii pu cs practical viva voce questions
Prof. Dr. K. Adisesha
 
Introduction to data_structure
Introduction to data_structureIntroduction to data_structure
Introduction to data_structure
Ashim Lamichhane
 
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
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueGhaffar Khan
 
Data structures using c
Data structures using cData structures using c
Data structures using c
Prof. Dr. K. Adisesha
 
Data structures using C
Data structures using CData structures using C
Data structures using C
Prof. Dr. K. Adisesha
 

What's hot (20)

Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Data structures
Data structuresData structures
Data structures
 
Lecture 2a arrays
Lecture 2a arraysLecture 2a arrays
Lecture 2a arrays
 
Data Structures Notes 2021
Data Structures Notes 2021Data Structures Notes 2021
Data Structures Notes 2021
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structure
 
Bsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structureBsc cs ii dfs u-1 introduction to data structure
Bsc cs ii dfs u-1 introduction to data structure
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
Data structure & algorithms introduction
Data structure & algorithms introductionData structure & algorithms introduction
Data structure & algorithms introduction
 
Mca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structureMca ii dfs u-1 introduction to data structure
Mca ii dfs u-1 introduction to data structure
 
Bca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structureBca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
 
Introduction to Data Structure
Introduction to Data Structure Introduction to Data Structure
Introduction to Data Structure
 
LectureNotes-03-DSA
LectureNotes-03-DSALectureNotes-03-DSA
LectureNotes-03-DSA
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1
 
Ii pu cs practical viva voce questions
Ii pu cs  practical viva voce questionsIi pu cs  practical viva voce questions
Ii pu cs practical viva voce questions
 
Introduction to data_structure
Introduction to data_structureIntroduction to data_structure
Introduction to data_structure
 
Lecture 3 data structures and algorithms
Lecture 3 data structures and algorithmsLecture 3 data structures and algorithms
Lecture 3 data structures and algorithms
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, Queue
 
Data structures using c
Data structures using cData structures using c
Data structures using c
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
LectureNotes-06-DSA
LectureNotes-06-DSALectureNotes-06-DSA
LectureNotes-06-DSA
 

Similar to Data structure lecture 2

ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptx
AmanRai352102
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
SivaSankar Gorantla
 
Arrays
ArraysArrays
Arrays
ViniVini48
 
Introduction-to-Arrays-in-Java . Exploring array
Introduction-to-Arrays-in-Java . Exploring arrayIntroduction-to-Arrays-in-Java . Exploring array
Introduction-to-Arrays-in-Java . Exploring array
AbdulSamad264371
 
unit 2.pptx
unit 2.pptxunit 2.pptx
unit 2.pptx
researchgrad82
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
Edureka!
 
Java R20 - UNIT-3.docx
Java R20 - UNIT-3.docxJava R20 - UNIT-3.docx
Java R20 - UNIT-3.docx
Pamarthi Kumar
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyMalikireddy Bramhananda Reddy
 
Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016) Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016)
Ameer B. Alaasam
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
Sony India Software Center
 
Any Which Array But Loose
Any Which Array But LooseAny Which Array But Loose
Any Which Array But Loose
michael.labriola
 
Ap Power Point Chpt6
Ap Power Point Chpt6Ap Power Point Chpt6
Ap Power Point Chpt6dplunkett
 
The actual illustration of values is decided by the machine design (.pdf
The actual illustration of values is decided by the machine design (.pdfThe actual illustration of values is decided by the machine design (.pdf
The actual illustration of values is decided by the machine design (.pdf
anyacarpets
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
MLG College of Learning, Inc
 
numpy.pdf
numpy.pdfnumpy.pdf

Similar to Data structure lecture 2 (20)

ppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptxppt on arrays in c programming language.pptx
ppt on arrays in c programming language.pptx
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
Arrays
ArraysArrays
Arrays
 
arrays.docx
arrays.docxarrays.docx
arrays.docx
 
Introduction-to-Arrays-in-Java . Exploring array
Introduction-to-Arrays-in-Java . Exploring arrayIntroduction-to-Arrays-in-Java . Exploring array
Introduction-to-Arrays-in-Java . Exploring array
 
unit 2.pptx
unit 2.pptxunit 2.pptx
unit 2.pptx
 
DSA-Lecture-05
DSA-Lecture-05DSA-Lecture-05
DSA-Lecture-05
 
Arrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | EdurekaArrays In Python | Python Array Operations | Edureka
Arrays In Python | Python Array Operations | Edureka
 
Java R20 - UNIT-3.docx
Java R20 - UNIT-3.docxJava R20 - UNIT-3.docx
Java R20 - UNIT-3.docx
 
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada ReddyDatastructures and algorithms prepared by M.V.Brehmanada Reddy
Datastructures and algorithms prepared by M.V.Brehmanada Reddy
 
Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016) Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016)
 
M v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notesM v bramhananda reddy dsa complete notes
M v bramhananda reddy dsa complete notes
 
Java Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
Any Which Array But Loose
Any Which Array But LooseAny Which Array But Loose
Any Which Array But Loose
 
Ap Power Point Chpt6
Ap Power Point Chpt6Ap Power Point Chpt6
Ap Power Point Chpt6
 
Java arrays (1)
Java arrays (1)Java arrays (1)
Java arrays (1)
 
The actual illustration of values is decided by the machine design (.pdf
The actual illustration of values is decided by the machine design (.pdfThe actual illustration of values is decided by the machine design (.pdf
The actual illustration of values is decided by the machine design (.pdf
 
Computer programming 2 Lesson 13
Computer programming 2  Lesson 13Computer programming 2  Lesson 13
Computer programming 2 Lesson 13
 
numpy.pdf
numpy.pdfnumpy.pdf
numpy.pdf
 
Collections
CollectionsCollections
Collections
 

More from Abbott

Chap007 MIS (Management Information System)
Chap007 MIS (Management Information System)Chap007 MIS (Management Information System)
Chap007 MIS (Management Information System)
Abbott
 
Chap006 MIS (Management Information System)
Chap006 MIS (Management Information System)Chap006 MIS (Management Information System)
Chap006 MIS (Management Information System)
Abbott
 
Chap005 MIS (Management Information System)
Chap005 MIS (Management Information System)Chap005 MIS (Management Information System)
Chap005 MIS (Management Information System)
Abbott
 
Chap004 MIS (Management Information System)
Chap004 MIS (Management Information System)Chap004 MIS (Management Information System)
Chap004 MIS (Management Information System)
Abbott
 
Chap003 MIS (Management Information System)
Chap003 MIS (Management Information System)Chap003 MIS (Management Information System)
Chap003 MIS (Management Information System)
Abbott
 
Chap002 (Management Information System)
Chap002 (Management Information System)Chap002 (Management Information System)
Chap002 (Management Information System)
Abbott
 
Chap001 MIS (Management Information System)
Chap001 MIS (Management Information System)Chap001 MIS (Management Information System)
Chap001 MIS (Management Information System)
Abbott
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queuesAbbott
 
Practice programs
Practice programsPractice programs
Practice programsAbbott
 
linked list
linked listlinked list
linked list
Abbott
 
Ch17
Ch17Ch17
Ch17
Abbott
 
Array 2
Array 2Array 2
Array 2
Abbott
 

More from Abbott (12)

Chap007 MIS (Management Information System)
Chap007 MIS (Management Information System)Chap007 MIS (Management Information System)
Chap007 MIS (Management Information System)
 
Chap006 MIS (Management Information System)
Chap006 MIS (Management Information System)Chap006 MIS (Management Information System)
Chap006 MIS (Management Information System)
 
Chap005 MIS (Management Information System)
Chap005 MIS (Management Information System)Chap005 MIS (Management Information System)
Chap005 MIS (Management Information System)
 
Chap004 MIS (Management Information System)
Chap004 MIS (Management Information System)Chap004 MIS (Management Information System)
Chap004 MIS (Management Information System)
 
Chap003 MIS (Management Information System)
Chap003 MIS (Management Information System)Chap003 MIS (Management Information System)
Chap003 MIS (Management Information System)
 
Chap002 (Management Information System)
Chap002 (Management Information System)Chap002 (Management Information System)
Chap002 (Management Information System)
 
Chap001 MIS (Management Information System)
Chap001 MIS (Management Information System)Chap001 MIS (Management Information System)
Chap001 MIS (Management Information System)
 
Stacks and queues
Stacks and queuesStacks and queues
Stacks and queues
 
Practice programs
Practice programsPractice programs
Practice programs
 
linked list
linked listlinked list
linked list
 
Ch17
Ch17Ch17
Ch17
 
Array 2
Array 2Array 2
Array 2
 

Recently uploaded

Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
ControlCase
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 
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
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Jeffrey Haguewood
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
DanBrown980551
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
Sri Ambati
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
Elena Simperl
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Albert Hoitingh
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
Prayukth K V
 
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
 
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
 
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
 
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
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
Cheryl Hung
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
Jemma Hussein Allen
 
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
 
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
 

Recently uploaded (20)

Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
PCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase TeamPCI PIN Basics Webinar from the Controlcase Team
PCI PIN Basics Webinar from the Controlcase Team
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 
Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........Bits & Pixels using AI for Good.........
Bits & Pixels using AI for Good.........
 
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
Slack (or Teams) Automation for Bonterra Impact Management (fka Social Soluti...
 
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
GenAISummit 2024 May 28 Sri Ambati Keynote: AGI Belongs to The Community in O...
 
When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...When stars align: studies in data quality, knowledge graphs, and machine lear...
When stars align: studies in data quality, knowledge graphs, and machine lear...
 
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024
 
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 previewState of ICS and IoT Cyber Threat Landscape Report 2024 preview
State of ICS and IoT Cyber Threat Landscape Report 2024 preview
 
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...
 
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
 
FIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdfFIDO Alliance Osaka Seminar: Overview.pdf
FIDO Alliance Osaka Seminar: Overview.pdf
 
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
 
Key Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdfKey Trends Shaping the Future of Infrastructure.pdf
Key Trends Shaping the Future of Infrastructure.pdf
 
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
The Future of Platform Engineering
The Future of Platform EngineeringThe Future of Platform Engineering
The Future of Platform Engineering
 
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
 
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...
 

Data structure lecture 2

  • 1. Data Structure Lecture 2 Following operations can be performed on the data structures: 1. Traversing 2. Searching 3. Inserting 4. Deleting 5. Sorting 6. Merging 1. Traversing- It is used to access each data item exactly once so that it can be processed. 2. Searching- It is used to find out the location of the data item if it exists in the given collection of data items. 3. Inserting- It is used to add a new data item in the given collection of data items. 4. Deleting- It is used to delete an existing data item from the given collection of data items. 5. Sorting- It is used to arrange the data items in some order i.e. in ascending or descending order in case of numerical data and in dictionary order in case of alphanumeric data. 6. Merging- It is used to combine the data items of two sorted files into single file in the sorted form. 1
  • 2. Array Data Structure Introduction An array is an aggregate data structure that is designed to store a group of objects of the same or different types. Arrays can hold primitives as well as references. The array is the most efficient data structure for storing and accessing a sequence of objects. Here is the list of most important array features you must know (i.e. be able to program) • copying and cloning • insertion and deletion • searching and sorting You already know that the Java language has only two data types, primitives and references. Which one is an array? Is it primitive? An array is not a primitive data type - it has a field (and only one), called length. Formally speaking, an array is a reference type, though you cannot find such a class in the Java APIs. Therefore, you deal with arrays as you deal with references. One of the major diffeences between refeences and primituives is that you cannot copy arrays by assigning one to another: int[] a = {9, 5, 4}; int[] b = a; The assignment operator creates an alias to the object, like in the picture below 2
  • 3. Since these two references a and b refer to the same object, comparing them with the double equal sign "==" will always return true. In the next code example, int [] a = {1,2,3}; int [] b = {1,2,3}; a and b refer to two different objects (though with identical contents). Comparing them with the double equal sign will return false. How would you compare two objects with identical contents? In short, using the equals method. For array comparison, the Java APIs provides the Arrays class. The Arrays class The java.util.Arrays class is a convenience class for various array manipulations, like comparison, searching, printing, sorting and others. Basically, this class is a set of static methods that are all useful for working with arrays. The code below demonstrates a proper invocation ofequals: int[] a = {1,2,3}; int[] b = {1,2,3}; if( Arrays.equals(a, b) ) System.out.println("arrays with identical contents"); 3
  • 4. Another commonly used method is toString() which takes care of of printing int[] a = {1,2,3}; System.out.println(Arrays.toString(a)); Here is the example of sorting int[] a = {3,2,1}; Arrays.sort(a); System.out.println(Arrays.toString(a)); In addition to that, the class has other utility methods for supporting operations over multidimensional arrays. Copying arrays There are four ways to copy arrays 1. using a loop structure 2. using Arrays.copyOf() 3. using System.arraycopy() 4. using clone() The first way is very well known to you int[] a = {1, 2, 3}; int[] b = new int[a.length]; for(int i = 0; i ‹ a.length; i++) b[i] = a[i]; The next choice is to use Arrays.copyOf() int[] a = {1, 2, 3}; int[] b = Arrays.copyOf(a, a.length); 4
  • 5. The second parameter specifies the length of the new array, which could either less or equal or bigger than the original length. The most efficient copying data between arrays is provided by System.arraycopy() method. The method requires five arguments. Here is its signature public static void arraycopy(Object source, int srcIndex, Object destination, int destIndex, int length) The method copies length elements from a source array starting with the index srcIndex to a new array destination at the indexdestIndex.The above code example can be rewritten as it follows int[] a = {1, 2, 3}; int[] b = new int[a.length]; System.arraycopy(a, 0, b, 0, 3) And the last copying choice is the use of cloning. Cloning involves creating a new array of the same size and type and copying all the old elements into the new array. The clone() method is defined in the Object class and its invocation is demonstrated by this code segment int[] a = {1, 2, 3}; int[] b = (int[]) a.clone(); 5
  • 6. The second parameter specifies the length of the new array, which could either less or equal or bigger than the original length. The most efficient copying data between arrays is provided by System.arraycopy() method. The method requires five arguments. Here is its signature public static void arraycopy(Object source, int srcIndex, Object destination, int destIndex, int length) The method copies length elements from a source array starting with the index srcIndex to a new array destination at the indexdestIndex.The above code example can be rewritten as it follows int[] a = {1, 2, 3}; int[] b = new int[a.length]; System.arraycopy(a, 0, b, 0, 3) And the last copying choice is the use of cloning. Cloning involves creating a new array of the same size and type and copying all the old elements into the new array. The clone() method is defined in the Object class and its invocation is demonstrated by this code segment int[] a = {1, 2, 3}; int[] b = (int[]) a.clone(); 5