Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.

Slides:



Advertisements
Similar presentations
Chapter 22 Implementing lists: linked implementations.
Advertisements

Lists and the Collection Interface Chapter 4. Chapter Objectives To become familiar with the List interface To understand how to write an array-based.
Arrays and ArrayLists Ananda Gunawardena. Introduction Array is a useful and powerful aggregate data structure presence in modern programming languages.
Designing an ADT The design of an ADT should evolve naturally during the problem-solving process Questions to ask when designing an ADT What data does.
Elementary Data Types Prof. Alamdeep Singh. Scalar Data Types Scalar data types represent a single object, i.e. only one value can be derived. In general,
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
Chapter Four Data Types Pratt 2 Data Objects A run-time grouping of one or more pieces of data in a virtual machine a container for data it can be –system.
Arrays.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
ISBN Chapter 6 Data Types: Structured types.
Multidimensional Arrays in Java Vidhu S. Kapadia.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Elementary Data Types Scalar Data Types Numerical Data Types Other
Aalborg Media Lab 28-Jun-15 Software Design Lecture 8 “Arrays”
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
Structured Data Types and Encapsulation Mechanisms to create new data types: –Structured data Homogeneous: arrays, lists, sets, Non-homogeneous: records.
Arrays Data Structures - structured data are data organized to show the relationship among the individual elements. It usually requires a collecting mechanism.
Arrays. The array data structure An array is an indexed sequence of components –Typically, the array occupies sequential storage locations –The length.
Chapter 9 Introduction to Arrays
Arrays. A problem with simple variables One variable holds one value –The value may change over time, but at any given time, a variable holds a single.
MT311 Java Application Programming and Programming Languages Li Tak Sing ( 李德成 )
Chapter 6Java: an Introduction to Computer Science & Programming - Walter Savitch 1 l Array Basics l Arrays in Classes and Methods l Programming with Arrays.
Processing Arrays Lesson 8 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Data Structures Winter What is a Data Structure? A data structure is a method of organizing data. The study of data structures is particularly important.
Lists in Python.
Object Oriented Programming in Java Habib Rostami Lecture 5.
Arrays. Arrays as ADTs An array is an Abstract Data Type –The array type has a set of values The values are all the possible arrays –The array type has.
ArrayList, Multidimensional Arrays
Chapter 11 Arrays Continued
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
1 © 2002, Cisco Systems, Inc. All rights reserved. Arrays Chapter 7.
Lec 6 Data types. Variable: Its data object that is defined and named by the programmer explicitly in a program. Data Types: It’s a class of Dos together.
Pointers. The structure of memory Computer memory is a linear sequence of addressable locations Addresses are numbered starting at zero In personal computers,
CPSC 252 Concrete Data Types Page 1 Overview of Concrete Data Types There are two kinds of data types: Simple (or atomic) – represents a single data item.
Data Structures and Algorithms Lecture 1 Instructor: Quratulain Date: 1 st Sep, 2009.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
Chapter overview This chapter focuses on Array declaration and use Bounds checking and capacity Arrays storing object references Variable length parameter.
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
© 2004 Pearson Addison-Wesley. All rights reserved October 15, 2007 Searching ComS 207: Programming I (in Java) Iowa State University, FALL 2007 Instructor:
JAVA: An Introduction to Problem Solving & Programming, 6 th Ed. By Walter Savitch ISBN © 2012 Pearson Education, Inc., Upper Saddle River,
Processing Arrays Lesson 9 McManusCOP Overview One-Dimensional Arrays –Entering Data into an Array –Printing an Array –Accumulating the elements.
Data Design and Implementation. Definitions Atomic or primitive type A data type whose elements are single, non-decomposable data items Composite type.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
Java Software Solutions Lewis and Loftus Chapter 6 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Objects for Organizing Data.
MT311 Java Application Development and Programming Languages Li Tak Sing ( 李德成 )
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
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 Structure and Algorithm: CIT231 Lecture 3: Arrays and ADT DeSiaMore DeSiaMorewww.desiamore.com/ifm1.
Arrays. 2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length.
Arrays. Arrays as ADTs An array is an Abstract Data Type –The array type has a set of values The values are all the possible arrays –The array type has.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
Chapter 9 Introduction to Arrays Fundamentals of Java.
Arrays An array is a sequence of objects all of which have the same type. The objects are called the elements of the array and are numbered consecutively.
CSI 3125, Data Types, page 1 Data types Outline Primitive data types Structured data types Strings Enumerated types Arrays Records Pointers Reading assignment.
Data Types In Text: Chapter 6.
Chapter 6 – Data Types CSCE 343.
Type Checking Generalizes the concept of operands and operators to include subprograms and assignments Type checking is the activity of ensuring that the.
Array, Strings and Vectors
Arrays.
ArrayLists 22-Feb-19.
Arrays.
ArrayLists 27-Apr-19.
Presentation transcript:

Arrays

2 The array data structure An array is an indexed sequence of components Typically, the array occupies sequential storage locations The length of the array is determined when the array is created, and cannot be changed Each component of the array has a fixed, unique index Indices range from a lower bound to an upper bound Any component of the array can be inspected or updated by using its index This is an efficient operation: O(1) = constant time

3 Array variations I The array indices may be integers (C, Java) or other discrete data types (Pascal, Ada) The lower bound may be zero (C, Java), one (Fortran), or chosen by the programmer (Pascal, Ada) In most languages, arrays are homogeneous (all components must have the same type); in some (Lisp, Prolog) the components may be heterogeneous (of varying types)

4 Array variations II In an object-oriented language, arrays may be objects (Java) or not objects (C++) Arrays may carry additional information about themselves, such as type and length (Java), or may consist only of their components (C, C++) I will use the terms reflective and non-reflective, respectively, to refer to these two types of arrays This is not standard terminology, but it is consistent with other uses of the terms

5 Arrays in Java I Array indices are integers An array of length n has bounds 0 and n-1 Arrays are homogeneous However, an array of an object type may contain objects of any subtype of that object For example, an array of Animal may contain objects of type Cat and objects of type Dog An array of Object may contain any type of object (but cannot contain primitives)

6 Arrays in Java II Arrays are objects Arrays are allocated by new, manipulated by reference, and garbage collected However, the usual bracket notation a[i] is provided as syntactic sugar Arrays are reflective a.length is the length of array a a.getClass() is the type of array a An array of integers has type [I An array of Strings has type [Ljava.lang.String;

7 Arrays in Java III Here’s one way to visualize an array in Java: myArray class tag [I length 4

8 Subarrays A subarray is a consecutive portion of an array Java provides no language support for subarrays To use a subarray, you must keep track of (1) the array itself, (2) the lower bound, and (3) the upper bound Typically, these will be three parameters to a method that does something with the subarray [I1055 array a subarray a[2...6]

9 Array as an ADT An array is an Abstract Data Type The array type has a set of values The values are all the possible arrays The array type has a set of operations that can be applied uniformly to each of these values The only operation is indexing Alternatively, this can be viewed as two operations: inspecting an indexed location storing into an indexed location It’s abstract because the implementation is hidden: all access is via the defined operations

10 Subarray as an ADT As noted earlier, to use a subarray, you must keep track of (1) the array itself, (2) the lower bound, and (3) the upper bound This suggests: class Subarray { V[ ] subarray; int lowerBound; int upperBound; // Constructor, some methods... } Advantage: Only one object to pass around Disadvantages: The subarray must hold Objects, not primitives You lose the nice array syntax

11 Two-dimensional arrays I Some languages (Fortran, Pascal) support two-dimensional (2D) arrays: A two-dimensional array may be stored in one-dimensional computer memory in either of two ways: acb ikj egf d l h logical view rows columns acbikjegfdlh row 0row 1row 2 row major order: aiekhdfcjblg col 0col 1col 2col 3 column major order:

12 Two-dimensional arrays II In a 2D array, we generally consider the first index to be the row, and the second to be the column: a[row, col] 0,00,10,20,30,4 1,01,11,21,31,4 2,02,12,22,32,4 3,03,13,23,33, columns rows In most languages we don’t need to know the implementation-- we work with this abstraction In C and C++, we do need to know the implementation

13 2D arrays in Java Java doesn’t have “real” 2D arrays, but array elements can themselves be arrays: int x[][] denotes an array x of array components, each of which is an array of integer components We can define the above array like this: x = new int[5][8]; and treat it as a regular 2D array This is an array of 5 arrays Each subarray is an array of 8 int s However, we can do fancier things than this with arrays in Java

14 Ragged arrays int ragged[][] = new int[4][]; for (int i = 0; i < 4; i++) { ragged[i] = new int[i + 1]; } for (int i = 0; i < 4; i++) { for (int j = 0; j < ragged[i].length; j++) { ragged[i][j] = 10 * i + j; } }

15 Inserting an element into an array Suppose we want to insert the value 8 into this sorted array (while keeping the array sorted) We can do this by shifting all the elements after the mark right by one location Of course, we have to discard the 30 when we do this Moving all those elements makes this a slow operation (linear in the size of the array)

16 Deleting an element from an array Deleting an element is similar--again, we have to move all the elements after it Deletion is a slow operation; we don’t want to do it very often Deletion leaves a “vacant” location at the end How do we mark it vacant? Every bit pattern represents a valid integer We must keep a count of how many valid elements are in the array ?

17 Arrays and ArrayLists Here are the advantages of arrays: Very efficient, in terms of both time and space Convenient syntax Here is the main disadvantage: Fixed size

18 Vectors and ArrayLists Vector methods include the following: public void insertElementAt(Object elem, int index) public void removeElementAt(int index) ArrayList methods include the following: public void add(int index, Object elem) public void remove(int index) These are slow (linear time) methods Should we avoid using these methods? Use them if this is the best way to make your program code simple and easier to understand Don’t use them if there is an equally simple alternative Don’t use them if extreme speed is required, and there is a faster alternative

19 Conclusions Arrays are not identical in all languages Arrays have the following advantages: Accessing an element by its index is very fast (constant time) Arrays have the following disadvantages: All elements must be of the same type The array size is fixed and can never be changed Insertion into arrays and deletion from arrays is very slow

20 The End Eclipse Hints ● If Eclipse persists in showing you syntax errors where you think there aren’t any, try choosing Project  Clean...