SlideShare a Scribd company logo
1 of 31
Download to read offline
Lesson 2 - Array
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM BY: AREGATON
Definition of Array
The array is the most commonly used data storage structure; it’s built into most programming
languages. Because arrays are so well known, they offer a convenient jumping-off place for
introducing data structures and for seeing how object-oriented programming and data
structures relate to one another.
Figure 2.1 shows the resulting array with 20 elements, 10 of which have data items in them. You
can think of these items as representing your baseball players. Imagine that each player has
been issued a team shirt with the player’s number on the back. To make things visually
interesting, the shirts come in a variety of colors. You can see each player’s number and shirt
color in the array.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 2
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 3
This applet demonstrates the three fundamental procedures mentioned earlier:
The Ins button inserts a new data item.
The Find button searches for specified data item.
The Del button deletes a specified data item
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 4
INSERTION
Start with the default arrangement of 20 cells and 10 data
items, and the No Dups button selected. You insert a baseball
player’s number into the array when the player arrives at the
practice field, having been dropped off by a parent. To insert a
new item, press the Ins button once. You’ll be prompted to
enter the value of the item:
Enter key of item to insert.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 5
Searching
To begin a search, click the Find button. You’ll be prompted for the
key number of the person you’re looking for. Pick a number that
appears on an item somewhere in the middle of the array. Type in
the number and repeatedly press the Find button. At each button
press, one step in the algorithm is carried out. You’ll see the red
arrow start at cell 0 and move methodically down the cells,
examining a new one each time you press the button. The index
number in the message -Checking next cell, index = 2
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 6
Deletion
To delete an item, you must first find it. After you type in the number of the item to be deleted,
repeated button presses will cause the arrow to move, step by step, down the array until the
item is located. The next button press deletes the item, and the cell becomes empty.
Implicit in the deletion algorithm is the assumption that holes are not allowed in the array. A
hole is one or more empty cells that have filled cells above them (at higher index numbers).
Therefore, after locating the specified item and deleting it, the applet must shift the contents of
each subsequent cell down one space to fill in the hole. Figure 2.2 shows an example.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 7
FLOW
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 8
The Duplicates Issue
When you design a data storage structure, you need to decide
whether items with duplicate keys will be allowed. If you’re working
with a personnel file and the key is an employee number, duplicates
don’t make much sense; there’s no point in assigning the same
number to two employees. On the other hand, if the key value is last
names, then there’s a distinct possibility several employees will have
the same key value, so duplicates should be allowed.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 9
Searching with Duplicates
Allowing duplicates complicates the search algorithm, as we noted.
Even if it finds a match, it must continue looking for possible
additional matches until the last occupied cell. At least, this is one
approach; you could also stop after the first match. How you
proceed depends on whether the question is “Find me everyone
with blue eyes” or “Find me someone with blue eyes.”
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 10
Insertion with Duplicates
Insertion is the same with duplicates allowed as when
they’re not: A single step inserts the new item. But
remember, if duplicates are not allowed, and there’s a
possibility the user will attempt to input the same key twice,
you may need to check every existing item before doing an
insertion.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 11
Deletion with Duplicates
Deletion may be more complicated when duplicates are allowed, depending on exactly how
“deletion” is defined. If it means to delete only the first item with a specified value, then, on the
average, only N/2 comparisons and N/2 moves are necessary. This is the same as when no
duplicates are allowed.
Table 2.1 shows the average number of comparisons and moves for the three operations, first
where no duplicates are allowed and then where they are allowed. N is the number of items in
the array. Inserting a new item counts as one move.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 12
The Basics of Arrays in Java
Creating an Array
There are two kinds of data in Java: primitive types (such as int and double) and objects. In many
programming languages (even object-oriented ones such as C++), arrays are primitive types, but
in Java they’re treated as objects. Accordingly, you must use the new operator to create an
array:
Or you can use the equivalent single-statement approach:
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 13
The Basics of Arrays in Java
Arrays have a length field, which you can use to find the size (the number of elements) of an
array:
Accessing Array Elements Array elements are accessed using an index number in square
brackets. This is similar to how other languages work:
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 14
Initialization
Unless you specify otherwise, an array of integers is automatically initialized to 0 when it’s
created. Unlike C++, this is true even of arrays defined within a method (function). Say you
create an array of objects like this:
You can initialize an array of a primitive type to something besides 0 using this syntax:
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 15
An Array Example
Let’s look at some example programs that show how an array can be used. We’ll start with an
old-fashioned procedural version and then show the equivalent object-oriented approach.
Listing 2.1 shows the old-fashioned version, called array.java.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 16
An Array Example
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 17
An Array Example
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 18
An Array Example
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 19
In this program, we create an array called arr, place 10 data items (kids’ numbers) in it, search
for the item with value 66 (the shortstop, Louisa), display all the items, remove the item with
value 55 (Freddy, who had a dentist appointment), and then display the remaining 9 items. The
output of the program looks like this:
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 20
Dividing a Program into Classes
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 21
Dividing a Program into Classes
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 22
Dividing a Program into Classes
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 23
Dividing a Program into Classes
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 24
OUTPUT:
The output from the lowArray.java program is similar to that from array.java, except that we try
to find a non-existent key value (26) before deleting the item with the key value 55:
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 25
Classes LowArray and LowArrayApp
In lowArray.java, we essentially wrap the class LowArray around an ordinary Java array. The
array is hidden from the outside world inside the class; it’s private, so only LowArray class
methods can access it. There are three LowArray methods: setElem() and getElem(), which
insert and retrieve an element, respectively; and a constructor, which creates an empty array of
a specified size.
Another class, LowArrayApp, creates an object of the LowArray class and uses it to store and
manipulate data. Think of LowArray as a tool and LowArrayApp as a user of the tool. We’ve
divided the program into two classes with clearly defined roles. This is a valuable first step in
making a program object oriented.
A class used to store data objects, as is LowArray in the lowArray.java program, is sometimes
called a container class. Typically, a container class not only stores the data but also provides
methods for accessing the data and perhaps also sorting it and performing other complex
actions on it.
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 26
The highArray.java Example
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 27
The highArray.java Example
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 28
The highArray.java Example
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 29
The highArray.java Example
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 30
OUTPUT
4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 31
The HighArray class is now wrapped around the array. In main(), we create an array of this class
and carry out almost the same operations as in the lowArray.java program: We insert 10 items,
search for an item—one that isn’t there—and display the array contents. Because deleting is so
easy, we delete 3 items (0, 55, and 99) instead of 1 and finally display the contents again. Here’s
the output:

More Related Content

What's hot

Data Structure and its Fundamentals
Data Structure and its FundamentalsData Structure and its Fundamentals
Data Structure and its FundamentalsHitesh Mohapatra
 
Data structures using C
Data structures using CData structures using C
Data structures using CPdr Patnaik
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparationKushaal Singla
 
ADT STACK and Queues
ADT STACK and QueuesADT STACK and Queues
ADT STACK and QueuesBHARATH KUMAR
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)Durga Devi
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)swajahatr
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its typesNavtar Sidhu Brar
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structureeShikshak
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#Shahzad
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST Kathirvel Ayyaswamy
 
Structure In C
Structure In CStructure In C
Structure In Cyndaravind
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2yndaravind
 
Database Management System-session 3-4-5
Database Management System-session 3-4-5Database Management System-session 3-4-5
Database Management System-session 3-4-5Infinity Tech Solutions
 
2 introduction to data structure
2  introduction to data structure2  introduction to data structure
2 introduction to data structureMahmoud Alfarra
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdNimmi Weeraddana
 

What's hot (20)

Data Structure
Data StructureData Structure
Data Structure
 
Data Structure and its Fundamentals
Data Structure and its FundamentalsData Structure and its Fundamentals
Data Structure and its Fundamentals
 
Data structures using C
Data structures using CData structures using C
Data structures using C
 
Unit i(dsc++)
Unit i(dsc++)Unit i(dsc++)
Unit i(dsc++)
 
Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)Computer Science-Data Structures :Abstract DataType (ADT)
Computer Science-Data Structures :Abstract DataType (ADT)
 
C interview-questions-techpreparation
C interview-questions-techpreparationC interview-questions-techpreparation
C interview-questions-techpreparation
 
ADT STACK and Queues
ADT STACK and QueuesADT STACK and Queues
ADT STACK and Queues
 
Unit iv(dsc++)
Unit iv(dsc++)Unit iv(dsc++)
Unit iv(dsc++)
 
Chapter 5 ds
Chapter 5 dsChapter 5 ds
Chapter 5 ds
 
linked list (c#)
 linked list (c#) linked list (c#)
linked list (c#)
 
Data structure and its types
Data structure and its typesData structure and its types
Data structure and its types
 
Introduction of data structure
Introduction of data structureIntroduction of data structure
Introduction of data structure
 
Data Structure In C#
Data Structure In C#Data Structure In C#
Data Structure In C#
 
UNIT I LINEAR DATA STRUCTURES – LIST
UNIT I 	LINEAR DATA STRUCTURES – LIST 	UNIT I 	LINEAR DATA STRUCTURES – LIST
UNIT I LINEAR DATA STRUCTURES – LIST
 
Chapter03
Chapter03Chapter03
Chapter03
 
Structure In C
Structure In CStructure In C
Structure In C
 
Fnctions part2
Fnctions part2Fnctions part2
Fnctions part2
 
Database Management System-session 3-4-5
Database Management System-session 3-4-5Database Management System-session 3-4-5
Database Management System-session 3-4-5
 
2 introduction to data structure
2  introduction to data structure2  introduction to data structure
2 introduction to data structure
 
Data structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pdData structures and algorithms short note (version 14).pd
Data structures and algorithms short note (version 14).pd
 

Similar to Array Lesson Guide

Algorithms.pptx
Algorithms.pptxAlgorithms.pptx
Algorithms.pptxjohn6938
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2Abbott
 
Lecture#1(Algorithmic Notations).ppt
Lecture#1(Algorithmic Notations).pptLecture#1(Algorithmic Notations).ppt
Lecture#1(Algorithmic Notations).pptMuhammadTalhaAwan1
 
The second programming assignment (HW4) is designed to help you ga.docx
The second programming assignment (HW4) is designed to help you ga.docxThe second programming assignment (HW4) is designed to help you ga.docx
The second programming assignment (HW4) is designed to help you ga.docxoreo10
 
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...PhD Assistance
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfkarymadelaneyrenne19
 
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.pptxAmanRai352102
 
XL-MINER:Data Exploration
XL-MINER:Data ExplorationXL-MINER:Data Exploration
XL-MINER:Data Explorationxlminer content
 
VB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.pptVB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.pptBhuvanaR13
 
Chapter 1 Introduction to Data Structures and Algorithms.pdf
Chapter 1 Introduction to Data Structures and Algorithms.pdfChapter 1 Introduction to Data Structures and Algorithms.pdf
Chapter 1 Introduction to Data Structures and Algorithms.pdfAxmedcarb
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfarpitaeron555
 
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...PhD Assistance
 
computer notes - Linked list inside computer memory
computer notes - Linked list inside computer memorycomputer notes - Linked list inside computer memory
computer notes - Linked list inside computer memoryecomputernotes
 
Bt0082 visual basic2
Bt0082 visual basic2Bt0082 visual basic2
Bt0082 visual basic2Techglyphs
 
computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stackecomputernotes
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#SyedUmairAli9
 

Similar to Array Lesson Guide (20)

Algorithms.pptx
Algorithms.pptxAlgorithms.pptx
Algorithms.pptx
 
Data structure lecture 2
Data structure lecture 2Data structure lecture 2
Data structure lecture 2
 
Lecture#1(Algorithmic Notations).ppt
Lecture#1(Algorithmic Notations).pptLecture#1(Algorithmic Notations).ppt
Lecture#1(Algorithmic Notations).ppt
 
The second programming assignment (HW4) is designed to help you ga.docx
The second programming assignment (HW4) is designed to help you ga.docxThe second programming assignment (HW4) is designed to help you ga.docx
The second programming assignment (HW4) is designed to help you ga.docx
 
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
 
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdfAbstract Data Types (a) Explain briefly what is meant by the ter.pdf
Abstract Data Types (a) Explain briefly what is meant by the ter.pdf
 
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
 
XL-MINER: Data Exploration
XL-MINER: Data ExplorationXL-MINER: Data Exploration
XL-MINER: Data Exploration
 
XL-MINER:Data Exploration
XL-MINER:Data ExplorationXL-MINER:Data Exploration
XL-MINER:Data Exploration
 
Any Which Array But Loose
Any Which Array But LooseAny Which Array But Loose
Any Which Array But Loose
 
VB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.pptVB_ERROR CONTROL_FILE HANDLING.ppt
VB_ERROR CONTROL_FILE HANDLING.ppt
 
Chapter 1 Introduction to Data Structures and Algorithms.pdf
Chapter 1 Introduction to Data Structures and Algorithms.pdfChapter 1 Introduction to Data Structures and Algorithms.pdf
Chapter 1 Introduction to Data Structures and Algorithms.pdf
 
Numerical data.
Numerical data.Numerical data.
Numerical data.
 
Introduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdfIntroduction and BackgroundIn recent lectures we discussed usi.pdf
Introduction and BackgroundIn recent lectures we discussed usi.pdf
 
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
How to Implement Sorting Algorithms in C++ and Difficulty Faced in Coding it?...
 
computer notes - Linked list inside computer memory
computer notes - Linked list inside computer memorycomputer notes - Linked list inside computer memory
computer notes - Linked list inside computer memory
 
Bt0082 visual basic2
Bt0082 visual basic2Bt0082 visual basic2
Bt0082 visual basic2
 
computer notes - Stack
computer notes - Stackcomputer notes - Stack
computer notes - Stack
 
Object Oriented Programming with C#
Object Oriented Programming with C#Object Oriented Programming with C#
Object Oriented Programming with C#
 
TSAT Presentation1.pptx
TSAT Presentation1.pptxTSAT Presentation1.pptx
TSAT Presentation1.pptx
 

More from MLG College of Learning, Inc (20)

PC111.Lesson2
PC111.Lesson2PC111.Lesson2
PC111.Lesson2
 
PC111.Lesson1
PC111.Lesson1PC111.Lesson1
PC111.Lesson1
 
PC111-lesson1.pptx
PC111-lesson1.pptxPC111-lesson1.pptx
PC111-lesson1.pptx
 
PC LEESOON 6.pptx
PC LEESOON 6.pptxPC LEESOON 6.pptx
PC LEESOON 6.pptx
 
PC 106 PPT-09.pptx
PC 106 PPT-09.pptxPC 106 PPT-09.pptx
PC 106 PPT-09.pptx
 
PC 106 PPT-07
PC 106 PPT-07PC 106 PPT-07
PC 106 PPT-07
 
PC 106 PPT-01
PC 106 PPT-01PC 106 PPT-01
PC 106 PPT-01
 
PC 106 PPT-06
PC 106 PPT-06PC 106 PPT-06
PC 106 PPT-06
 
PC 106 PPT-05
PC 106 PPT-05PC 106 PPT-05
PC 106 PPT-05
 
PC 106 Slide 04
PC 106 Slide 04PC 106 Slide 04
PC 106 Slide 04
 
PC 106 Slide no.02
PC 106 Slide no.02PC 106 Slide no.02
PC 106 Slide no.02
 
pc-106-slide-3
pc-106-slide-3pc-106-slide-3
pc-106-slide-3
 
PC 106 Slide 2
PC 106 Slide 2PC 106 Slide 2
PC 106 Slide 2
 
PC 106 Slide 1.pptx
PC 106 Slide 1.pptxPC 106 Slide 1.pptx
PC 106 Slide 1.pptx
 
Db2 characteristics of db ms
Db2 characteristics of db msDb2 characteristics of db ms
Db2 characteristics of db ms
 
Db1 introduction
Db1 introductionDb1 introduction
Db1 introduction
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 
Lesson 3.1
Lesson 3.1Lesson 3.1
Lesson 3.1
 
Lesson 1.6
Lesson 1.6Lesson 1.6
Lesson 1.6
 
Lesson 3.2
Lesson 3.2Lesson 3.2
Lesson 3.2
 

Recently uploaded

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfadityarao40181
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for BeginnersSabitha Banu
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon AUnboundStockton
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)eniolaolutunde
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfSumit Tiwari
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...Marc Dusseiller Dusjagr
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerunnathinaik
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Celine George
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 

Recently uploaded (20)

Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Biting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdfBiting mechanism of poisonous snakes.pdf
Biting mechanism of poisonous snakes.pdf
 
Full Stack Web Development Course for Beginners
Full Stack Web Development Course  for BeginnersFull Stack Web Development Course  for Beginners
Full Stack Web Development Course for Beginners
 
Crayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon ACrayon Activity Handout For the Crayon A
Crayon Activity Handout For the Crayon A
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)Software Engineering Methodologies (overview)
Software Engineering Methodologies (overview)
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdfEnzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
Enzyme, Pharmaceutical Aids, Miscellaneous Last Part of Chapter no 5th.pdf
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
“Oh GOSH! Reflecting on Hackteria's Collaborative Practices in a Global Do-It...
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
internship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developerinternship ppt on smartinternz platform as salesforce developer
internship ppt on smartinternz platform as salesforce developer
 
Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17Computed Fields and api Depends in the Odoo 17
Computed Fields and api Depends in the Odoo 17
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 

Array Lesson Guide

  • 1. Lesson 2 - Array 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM BY: AREGATON
  • 2. Definition of Array The array is the most commonly used data storage structure; it’s built into most programming languages. Because arrays are so well known, they offer a convenient jumping-off place for introducing data structures and for seeing how object-oriented programming and data structures relate to one another. Figure 2.1 shows the resulting array with 20 elements, 10 of which have data items in them. You can think of these items as representing your baseball players. Imagine that each player has been issued a team shirt with the player’s number on the back. To make things visually interesting, the shirts come in a variety of colors. You can see each player’s number and shirt color in the array. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 2
  • 3. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 3
  • 4. This applet demonstrates the three fundamental procedures mentioned earlier: The Ins button inserts a new data item. The Find button searches for specified data item. The Del button deletes a specified data item 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 4
  • 5. INSERTION Start with the default arrangement of 20 cells and 10 data items, and the No Dups button selected. You insert a baseball player’s number into the array when the player arrives at the practice field, having been dropped off by a parent. To insert a new item, press the Ins button once. You’ll be prompted to enter the value of the item: Enter key of item to insert. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 5
  • 6. Searching To begin a search, click the Find button. You’ll be prompted for the key number of the person you’re looking for. Pick a number that appears on an item somewhere in the middle of the array. Type in the number and repeatedly press the Find button. At each button press, one step in the algorithm is carried out. You’ll see the red arrow start at cell 0 and move methodically down the cells, examining a new one each time you press the button. The index number in the message -Checking next cell, index = 2 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 6
  • 7. Deletion To delete an item, you must first find it. After you type in the number of the item to be deleted, repeated button presses will cause the arrow to move, step by step, down the array until the item is located. The next button press deletes the item, and the cell becomes empty. Implicit in the deletion algorithm is the assumption that holes are not allowed in the array. A hole is one or more empty cells that have filled cells above them (at higher index numbers). Therefore, after locating the specified item and deleting it, the applet must shift the contents of each subsequent cell down one space to fill in the hole. Figure 2.2 shows an example. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 7
  • 8. FLOW 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 8
  • 9. The Duplicates Issue When you design a data storage structure, you need to decide whether items with duplicate keys will be allowed. If you’re working with a personnel file and the key is an employee number, duplicates don’t make much sense; there’s no point in assigning the same number to two employees. On the other hand, if the key value is last names, then there’s a distinct possibility several employees will have the same key value, so duplicates should be allowed. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 9
  • 10. Searching with Duplicates Allowing duplicates complicates the search algorithm, as we noted. Even if it finds a match, it must continue looking for possible additional matches until the last occupied cell. At least, this is one approach; you could also stop after the first match. How you proceed depends on whether the question is “Find me everyone with blue eyes” or “Find me someone with blue eyes.” 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 10
  • 11. Insertion with Duplicates Insertion is the same with duplicates allowed as when they’re not: A single step inserts the new item. But remember, if duplicates are not allowed, and there’s a possibility the user will attempt to input the same key twice, you may need to check every existing item before doing an insertion. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 11
  • 12. Deletion with Duplicates Deletion may be more complicated when duplicates are allowed, depending on exactly how “deletion” is defined. If it means to delete only the first item with a specified value, then, on the average, only N/2 comparisons and N/2 moves are necessary. This is the same as when no duplicates are allowed. Table 2.1 shows the average number of comparisons and moves for the three operations, first where no duplicates are allowed and then where they are allowed. N is the number of items in the array. Inserting a new item counts as one move. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 12
  • 13. The Basics of Arrays in Java Creating an Array There are two kinds of data in Java: primitive types (such as int and double) and objects. In many programming languages (even object-oriented ones such as C++), arrays are primitive types, but in Java they’re treated as objects. Accordingly, you must use the new operator to create an array: Or you can use the equivalent single-statement approach: 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 13
  • 14. The Basics of Arrays in Java Arrays have a length field, which you can use to find the size (the number of elements) of an array: Accessing Array Elements Array elements are accessed using an index number in square brackets. This is similar to how other languages work: 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 14
  • 15. Initialization Unless you specify otherwise, an array of integers is automatically initialized to 0 when it’s created. Unlike C++, this is true even of arrays defined within a method (function). Say you create an array of objects like this: You can initialize an array of a primitive type to something besides 0 using this syntax: 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 15
  • 16. An Array Example Let’s look at some example programs that show how an array can be used. We’ll start with an old-fashioned procedural version and then show the equivalent object-oriented approach. Listing 2.1 shows the old-fashioned version, called array.java. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 16
  • 17. An Array Example 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 17
  • 18. An Array Example 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 18
  • 19. An Array Example 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 19
  • 20. In this program, we create an array called arr, place 10 data items (kids’ numbers) in it, search for the item with value 66 (the shortstop, Louisa), display all the items, remove the item with value 55 (Freddy, who had a dentist appointment), and then display the remaining 9 items. The output of the program looks like this: 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 20
  • 21. Dividing a Program into Classes 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 21
  • 22. Dividing a Program into Classes 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 22
  • 23. Dividing a Program into Classes 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 23
  • 24. Dividing a Program into Classes 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 24
  • 25. OUTPUT: The output from the lowArray.java program is similar to that from array.java, except that we try to find a non-existent key value (26) before deleting the item with the key value 55: 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 25
  • 26. Classes LowArray and LowArrayApp In lowArray.java, we essentially wrap the class LowArray around an ordinary Java array. The array is hidden from the outside world inside the class; it’s private, so only LowArray class methods can access it. There are three LowArray methods: setElem() and getElem(), which insert and retrieve an element, respectively; and a constructor, which creates an empty array of a specified size. Another class, LowArrayApp, creates an object of the LowArray class and uses it to store and manipulate data. Think of LowArray as a tool and LowArrayApp as a user of the tool. We’ve divided the program into two classes with clearly defined roles. This is a valuable first step in making a program object oriented. A class used to store data objects, as is LowArray in the lowArray.java program, is sometimes called a container class. Typically, a container class not only stores the data but also provides methods for accessing the data and perhaps also sorting it and performing other complex actions on it. 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 26
  • 27. The highArray.java Example 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 27
  • 28. The highArray.java Example 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 28
  • 29. The highArray.java Example 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 29
  • 30. The highArray.java Example 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 30
  • 31. OUTPUT 4/4/2021 PC-108 DATA STRUCTURE AND ALGORITHM 31 The HighArray class is now wrapped around the array. In main(), we create an array of this class and carry out almost the same operations as in the lowArray.java program: We insert 10 items, search for an item—one that isn’t there—and display the array contents. Because deleting is so easy, we delete 3 items (0, 55, and 99) instead of 1 and finally display the contents again. Here’s the output: