Array.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Introduction to C Programming
CHP-5 LinkedList.
Searching for Data Relationship between searching and sorting Simple linear searching Linear searching of sorted data Searching for string or numeric data.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Overview of Data Structures and Algorithms
Generics and the ArrayList Class
Overloading Operators Object-Oriented Programming Using C++ Second Edition 8.
Lecture 3 Feb 4 summary of last week’s topics and review questions (handout) Today’s goals: Chapter 1 overview (sections 1.4 to 1.6) c++ classes constructors,
1 Problem Solving Abstraction Oftentimes, different real-world problems can be modeled using the same underlying idea Examples: Runtime storage, Undo operation.
 2006 Pearson Education, Inc. All rights reserved Introduction to Classes and Objects.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Sets and Maps Chapter 9. Chapter 9: Sets and Maps2 Chapter Objectives To understand the Java Map and Set interfaces and how to use them To learn about.
Introduction to Computers and Programming Lecture 15: Arrays Professor: Evan Korth New York University.
© 2006 Pearson Addison-Wesley. All rights reserved4-1 Chapter 4 Data Abstraction: The Walls.
Slides prepared by Rose Williams, Binghamton University Chapter 14 Generics and the ArrayList Class.
Chapter 14 Generics and the ArrayList Class Copyright © 2010 Pearson Addison-Wesley. All rights reserved.
26-Jun-15 Arrays. 2 A problem with simple variables One variable holds one value The value may change over time, but at any given time, a variable holds.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 9 Objects and Classes.
Chapter 9 Introduction to Arrays
1 ES 314 Advanced Programming Lec 2 Sept 3 Goals: Complete the discussion of problem Review of C++ Object-oriented design Arrays and pointers.
C++ fundamentals.
Stacks, Queues, and Deques
1.A file is organized logically as a sequence of records. 2. These records are mapped onto disk blocks. 3. Files are provided as a basic construct in operating.
Hashing 1. Def. Hash Table an array in which items are inserted according to a key value (i.e. the key value is used to determine the index of the item).
MT311 Java Programming and Programming Languages Li Tak Sing ( 李德成 )
Tree.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Chapter 10 Strings, Searches, Sorts, and Modifications Midterm Review By Ben Razon AP Computer Science Period 3.
Chapter 2 Array Data Structure Winter Array The Array is the most commonly used Data Storage Structure. It’s built into most Programming languages.
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
An Object-Oriented Approach to Programming Logic and Design Fourth Edition Chapter 5 Arrays.
Chapter 18 Java Collections Framework
Chapter 14 Generics and the ArrayList Class Slides prepared by Rose Williams, Binghamton University Copyright © 2008 Pearson Addison-Wesley. All rights.
Data structures Abstract data types Java classes for Data structures and ADTs.
Linked List. Background Arrays has certain disadvantages as data storage structures. ▫In an unordered array, searching is slow ▫In an ordered array, insertion.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
Topic 1 Object Oriented Programming. 1-2 Objectives To review the concepts and terminology of object-oriented programming To discuss some features of.
Hashing Hashing is another method for sorting and searching data.
Linked List. Iterators Operation to find a link, deleting, and inserting before or after a specified link, also involve searching through the list to.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Standard Template Library The Standard Template Library was recently added to standard C++. –The STL contains generic template classes. –The STL permits.
1 CSCD 326 Data Structures I Software Design. 2 The Software Life Cycle 1. Specification 2. Design 3. Risk Analysis 4. Verification 5. Coding 6. Testing.
Lecture 26: Reusable Methods: Enviable Sloth. Creating Function M-files User defined functions are stored as M- files To use them, they must be in the.
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
M1G Introduction to Programming 2 3. Creating Classes: Room and Item.
Programming Fundamentals. Topics to be covered Today Recursion Inline Functions Scope and Storage Class A simple class Constructor Destructor.
Chapter 5 Linked List by Before you learn Linked List 3 rd level of Data Structures Intermediate Level of Understanding for C++ Please.
CSE 1201 Object Oriented Programming ArrayList 1.
Copyright (c) 2014 Pearson Education, Inc. Introduction to DBMS.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
Data Structures Arrays and Lists Part 2 More List Operations.
Sets and Maps Chapter 9. Chapter Objectives  To understand the Java Map and Set interfaces and how to use them  To learn about hash coding and its use.
Programming Logic and Design Fifth Edition, Comprehensive Chapter 6 Arrays.
Program Design. Simple Program Design, Fourth Edition Chapter 1 2 Objectives In this chapter you will be able to: Describe the steps in the program development.
LINKED LISTS.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Copyright © 2002 Pearson Education, Inc. Slide 1.
Arrays Chapter 7.
Fundamentals of Java: AP Computer Science Essentials, 4th Edition
Chapter 7 Part 1 Edited by JJ Shepherd
Array.
ArrayLists 22-Feb-19.
Introduction to Data Structure
Presentation transcript:

Array

Definition Array is the most commonly used data storage structure; it’s built into most programming languages.

Array Operation Insertion Searching Deletion

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.

The Duplicates Issue (Cont’) If you’re writing a data storage program in which duplicates are not allowed, you may need to guard against human error during an insertion by checking all the data items in the array to ensure that none of them already has the same key value as the item being inserted. This check is inefficient, however, and increases the number of steps required for an insertion

The Duplicates Issue (Cont’) Searching with Duplicates Allowing duplicates complicates the search algorithm. Even if it finds a match, it must continue looking for possible additional matches until the last occupied cell. Insertion with Duplicates Insertion with duplicates allowed only need a single step to 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. Deletion with Duplicates Deletion may be more complicated when duplicates are allowed, depending on exactly how “deletion” is defined. If, however, deletion means to delete every item with a specified key value, the same operation may require multiple deletions.

The Basics of Arrays in Java in Java Array were treated as objects. Accordingly, you must use the new operator to create an array: int[] intArray; // defines a reference to an array intArray = new int[100]; // creates the array, and sets intArray to refer to it Or you can use the equivalent single-statement approach: int[] intArray = new int[100]; Arrays have a length field, which you can use to find the size (the number of elements) of an array: int arrayLength = intArray.length; // find array size

Accessing Array Elements Array elements are accessed using an index number in square brackets. This is similar to how other languages work: temp = intArray[3]; // get contents of fourth element of array intArray[7] = 66; // insert 66 into the eighth cell Remember that in Java, as in C and C++, the first element is numbered 0, so that the indices in an array of 10 elements run from 0 to 9. If you use an index that’s less than 0 or greater than the size of the array less 1, you’ll get the Array Index Out of Bounds runtime error.

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: autoData[] carArray = new autoData[4000]; Until the array elements are given explicit values, they contain the special null object. If you attempt to access an array element that contains null, you’ll get the runtime error Null Pointer Assignment. The moral is to make sure you assign something to an element before attempting to access it. You can initialize an array of a primitive type to something besides 0 using this syntax: int[] intArray = { 0, 3, 6, 9, 12, 15, 18, 21, 24, 27 };

Example 1

Dividing a Program into Classes The array.java program in Listing 2.1 essentially consists of one big method. We can reap many benefits by dividing the program into classes. The data storage structure The part of the program that uses this data structure By dividing the program into these two classes, we can clarify the functionality of the program, making it easier to design and understand (and in real programs to modify and maintain).

Dividing a Program into Classes(Con’t) 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 it

Class Interfaces We’ve seen how a program can be divided into separate classes. How do these classes interact with each other? Communication between classes and the division of responsibility between them are important aspects of object-oriented programming. This point is especially true when a class may have many different users. Typically, a class can be used over and over by different users (or the same user) for different purposes. If a class is used by many different programmers, the class should be designed so that it’s easy to use. The way that a class user relates to the class is called the class interface. Because class fields are typically private, when we talk about the interface, we usually mean the class methods—what they do and what their arguments are. By calling these methods, a class user interacts with an object of the class. One of the important advantages conferred by object-oriented programming is that a class interface can be designed to be as convenient and efficient as possible.

Class Interfaces(cont’) The interface to the LowArray class in lowArray.java is not particularly convenient. The methods setElem() and getElem() operate on a low conceptual level, performing exactly the same tasks as the [] operator in an ordinary Java array. The class user, represented by the main() method in the LowArrayApp class, ends up having to carry out the same low-level operations it did in the non-class version of an array in the array.java program. The only difference was that it related to setElem() and getElem() instead of the [] operator. It’s not clear that this approach is an improvement.

conclusion Using this interface, the class user (the HighArrayApp class) no longer needs to think about index numbers. The setElem() and getElem() methods are gone; they’re replaced by insert(), find(), and delete(). These new methods don’t require an index number as an argument because the class takes responsibility for handling index numbers. The user of the class (HighArrayApp) is free to concentrate on the what instead of the how—what’s going to be inserted, deleted, and accessed, instead of exactly how these activities are carried out. Notice how short and simple main() is. The details that had to be handled by main() in lowArray.java are now handled by HighArray class methods.