SlideShare une entreprise Scribd logo
1  sur  5
Télécharger pour lire hors ligne
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.
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
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");
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);
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();

Contenu connexe

Tendances

Data structure & algorithms introduction
Data structure & algorithms introductionData structure & algorithms introduction
Data structure & algorithms introductionSugandh Wafai
 
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 structureRai University
 
Data structures Basics
Data structures BasicsData structures Basics
Data structures BasicsDurgaDeviCbit
 
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 structureRai University
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structureVivek Kumar Sinha
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueGhaffar Khan
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structureeShikshak
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1Kumar
 
2 introduction to data structure
2  introduction to data structure2  introduction to data structure
2 introduction to data structureMahmoud Alfarra
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)Arvind Devaraj
 
Introduction of data structures and algorithms
Introduction of data structures and algorithmsIntroduction of data structures and algorithms
Introduction of data structures and algorithmsVinayKumarV16
 
Data Structures Notes 2021
Data Structures Notes 2021Data Structures Notes 2021
Data Structures Notes 2021Sreedhar Chowdam
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure NUPOORAWSARMOL
 

Tendances (20)

Data structure & algorithms introduction
Data structure & algorithms introductionData structure & algorithms introduction
Data structure & algorithms introduction
 
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
 
Data structures Basics
Data structures BasicsData structures Basics
Data structures Basics
 
Data structures
Data structuresData structures
Data structures
 
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
 
Data Structure Basics
Data Structure BasicsData Structure Basics
Data Structure Basics
 
Arrays Basics
Arrays BasicsArrays Basics
Arrays Basics
 
Lecture 2a arrays
Lecture 2a arraysLecture 2a arrays
Lecture 2a arrays
 
Data structure and its types.
Data structure and its types.Data structure and its types.
Data structure and its types.
 
Introduction to data structure
Introduction to data structureIntroduction to data structure
Introduction to data structure
 
Introductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, QueueIntroductiont To Aray,Tree,Stack, Queue
Introductiont To Aray,Tree,Stack, Queue
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structure
 
Data structure lecture 1
Data structure lecture 1Data structure lecture 1
Data structure lecture 1
 
2 introduction to data structure
2  introduction to data structure2  introduction to data structure
2 introduction to data structure
 
Data structures (introduction)
 Data structures (introduction) Data structures (introduction)
Data structures (introduction)
 
Introduction of data structures and algorithms
Introduction of data structures and algorithmsIntroduction of data structures and algorithms
Introduction of data structures and algorithms
 
Data Structures Notes 2021
Data Structures Notes 2021Data Structures Notes 2021
Data Structures Notes 2021
 
Data structures
Data structuresData structures
Data structures
 
Data structures Lecture 5
Data structures Lecture 5Data structures Lecture 5
Data structures Lecture 5
 
Introduction to data structure
Introduction to data structure Introduction to data structure
Introduction to data structure
 

Similaire à Data structure lecture 2 (pdf)

Similaire à Data structure lecture 2 (pdf) (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
 
unit 2.pptx
unit 2.pptxunit 2.pptx
unit 2.pptx
 
Data structures in c#
Data structures in c#Data structures in c#
Data structures in c#
 
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 Collections Framework
Java Collections FrameworkJava Collections Framework
Java Collections Framework
 
arrays.docx
arrays.docxarrays.docx
arrays.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
 
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
 
Arrays
ArraysArrays
Arrays
 
Any Which Array But Loose
Any Which Array But LooseAny Which Array But Loose
Any Which Array But Loose
 
Arrays Introduction.pptx
Arrays Introduction.pptxArrays Introduction.pptx
Arrays Introduction.pptx
 
01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptx01-Introduction of DSA-1.pptx
01-Introduction of DSA-1.pptx
 
Unit 2 linear data structures
Unit 2   linear data structuresUnit 2   linear data structures
Unit 2 linear data structures
 
8074.pdf
8074.pdf8074.pdf
8074.pdf
 
Java R20 - UNIT-3.docx
Java R20 - UNIT-3.docxJava R20 - UNIT-3.docx
Java R20 - UNIT-3.docx
 
Ap Power Point Chpt6
Ap Power Point Chpt6Ap Power Point Chpt6
Ap Power Point Chpt6
 
Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016) Data structures "1" (Lectures 2015-2016)
Data structures "1" (Lectures 2015-2016)
 
DSA-Lecture-05
DSA-Lecture-05DSA-Lecture-05
DSA-Lecture-05
 
Lesson 2.2 abstraction
Lesson 2.2   abstractionLesson 2.2   abstraction
Lesson 2.2 abstraction
 
Collections framework
Collections frameworkCollections framework
Collections framework
 

Plus de 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 listAbbott
 
Array 2
Array 2Array 2
Array 2Abbott
 

Plus de 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
 

Dernier

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProduct Anonymous
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 

Dernier (20)

Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemkeProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
ProductAnonymous-April2024-WinProductDiscovery-MelissaKlemke
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 

Data structure lecture 2 (pdf)

  • 1. 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.
  • 2. 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
  • 3. 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");
  • 4. 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);
  • 5. 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();