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

PDF
Data structure lecture 2 (pdf)
PPT
Data Structure In C#
PDF
Data Structure Basics
PPTX
Data Structure & Algorithms | Computer Science
PPTX
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
PPT
Abstract data types
PPT
Data structures
PPTX
ARRAY
Data structure lecture 2 (pdf)
Data Structure In C#
Data Structure Basics
Data Structure & Algorithms | Computer Science
Data structure,abstraction,abstract data type,static and dynamic,time and spa...
Abstract data types
Data structures
ARRAY

What's hot (20)

PPTX
Data structure and its types
PPTX
PPT
Lecture 2a arrays
PDF
Data Structures Notes 2021
PPT
Introduction of data structure
PPTX
Bsc cs ii dfs u-1 introduction to data structure
PPT
Arrays Basics
PPTX
Data structure & algorithms introduction
PPTX
Mca ii dfs u-1 introduction to data structure
PPTX
Bca ii dfs u-1 introduction to data structure
PDF
Introduction to Data Structure
PDF
LectureNotes-03-DSA
PPT
Data structure lecture 1
PDF
Ii pu cs practical viva voce questions
PPTX
Introduction to data_structure
PPTX
Lecture 3 data structures and algorithms
PPT
Introductiont To Aray,Tree,Stack, Queue
PPT
Data structures using c
PPT
Data structures using C
PDF
LectureNotes-06-DSA
Data structure and its types
Lecture 2a arrays
Data Structures Notes 2021
Introduction of data structure
Bsc cs ii dfs u-1 introduction to data structure
Arrays Basics
Data structure & algorithms introduction
Mca ii dfs u-1 introduction to data structure
Bca ii dfs u-1 introduction to data structure
Introduction to Data Structure
LectureNotes-03-DSA
Data structure lecture 1
Ii pu cs practical viva voce questions
Introduction to data_structure
Lecture 3 data structures and algorithms
Introductiont To Aray,Tree,Stack, Queue
Data structures using c
Data structures using C
LectureNotes-06-DSA
Ad

Similar to Data structure lecture 2 (20)

PDF
Java arrays (1)
PPTX
Computer programming 2 Lesson 13
PPT
Arrays Basicfundamentaldatastructure.ppt
PPT
17-Arrays en java presentación documento
PPTX
07+08slide.pptx
PPTX
Java arrays
PPT
Eo gaddis java_chapter_07_5e
PDF
Java Arrays
PPTX
DOCX
Java R20 - UNIT-3.docx
PDF
Lecture 6 - Arrays
PPT
ch06.ppt
PPT
PPT
ch06.ppt
PPT
array Details
PPT
Data Structure Midterm Lesson Arrays
PPTX
Arrays in programming
PPT
Array
PDF
Java chapter 6 - Arrays -syntax and use
PPT
Java căn bản - Chapter10
Java arrays (1)
Computer programming 2 Lesson 13
Arrays Basicfundamentaldatastructure.ppt
17-Arrays en java presentación documento
07+08slide.pptx
Java arrays
Eo gaddis java_chapter_07_5e
Java Arrays
Java R20 - UNIT-3.docx
Lecture 6 - Arrays
ch06.ppt
ch06.ppt
array Details
Data Structure Midterm Lesson Arrays
Arrays in programming
Array
Java chapter 6 - Arrays -syntax and use
Java căn bản - Chapter10
Ad

More from Abbott (12)

PPT
Chap007 MIS (Management Information System)
PPT
Chap006 MIS (Management Information System)
PPT
Chap005 MIS (Management Information System)
PPT
Chap004 MIS (Management Information System)
PPT
Chap003 MIS (Management Information System)
PPT
Chap002 (Management Information System)
PPT
Chap001 MIS (Management Information System)
PDF
Stacks and queues
PDF
Practice programs
PDF
linked list
PPT
Ch17
PPT
Array 2
Chap007 MIS (Management Information System)
Chap006 MIS (Management Information System)
Chap005 MIS (Management Information System)
Chap004 MIS (Management Information System)
Chap003 MIS (Management Information System)
Chap002 (Management Information System)
Chap001 MIS (Management Information System)
Stacks and queues
Practice programs
linked list
Ch17
Array 2

Recently uploaded (20)

PDF
Approach and Philosophy of On baking technology
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PDF
Spectral efficient network and resource selection model in 5G networks
PDF
KodekX | Application Modernization Development
PPTX
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Reach Out and Touch Someone: Haptics and Empathic Computing
PPTX
Cloud computing and distributed systems.
PDF
Encapsulation_ Review paper, used for researhc scholars
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PPTX
Spectroscopy.pptx food analysis technology
PDF
Dropbox Q2 2025 Financial Results & Investor Presentation
PPTX
sap open course for s4hana steps from ECC to s4
PDF
Review of recent advances in non-invasive hemoglobin estimation
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Empathic Computing: Creating Shared Understanding
PDF
Advanced methodologies resolving dimensionality complications for autism neur...
Approach and Philosophy of On baking technology
MIND Revenue Release Quarter 2 2025 Press Release
Spectral efficient network and resource selection model in 5G networks
KodekX | Application Modernization Development
KOM of Painting work and Equipment Insulation REV00 update 25-dec.pptx
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Reach Out and Touch Someone: Haptics and Empathic Computing
Cloud computing and distributed systems.
Encapsulation_ Review paper, used for researhc scholars
The AUB Centre for AI in Media Proposal.docx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Spectroscopy.pptx food analysis technology
Dropbox Q2 2025 Financial Results & Investor Presentation
sap open course for s4hana steps from ECC to s4
Review of recent advances in non-invasive hemoglobin estimation
20250228 LYD VKU AI Blended-Learning.pptx
Architecting across the Boundaries of two Complex Domains - Healthcare & Tech...
Digital-Transformation-Roadmap-for-Companies.pptx
Empathic Computing: Creating Shared Understanding
Advanced methodologies resolving dimensionality complications for autism neur...

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