CMSC131. Arrays: The Concept

Similar documents
CMSC131. Seating Chart

CMSC131. A Simple Problem?

Object Oriented Programming. Java-Lecture 6 - Arrays

CMSC131. How Objects Can (or Can't) Change

Chapter 9 Introduction to Arrays. Fundamentals of Java

Array Basics: Outline. Creating and Accessing Arrays. Creating and Accessing Arrays. Arrays (Savitch, Chapter 7)

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

CONTENTS: Array Usage Multi-Dimensional Arrays Reference Types. COMP-202 Unit 6: Arrays

Arrays. Eng. Mohammed Abdualal

Last Class. Introduction to arrays Array indices Initializer lists Making an array when you don't know how many values are in it

Last Class. More on loops break continue A bit on arrays

array Indexed same type

2-D Arrays. Of course, to set each grid location to 0, we have to use a loop structure as follows (assume i and j are already defined):

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #43. Multidimensional Arrays

Arrays: Higher Dimensional Arrays. CS0007: Introduction to Computer Programming

1B1a Arrays. Arrays. Indexing. Naming arrays. Why? Using indexing. 1B1a Lecture Slides. Copyright 2003, Graham Roberts 1

Objectives. Order (sort) the elements of an array Search an array for a particular item Define, use multidimensional array

Arrays. Lecture 11 CGS 3416 Spring March 6, Lecture 11CGS 3416 Spring 2017 Arrays March 6, / 19

FOR Loop. FOR Loop has three parts:initialization,condition,increment. Syntax. for(initialization;condition;increment){ body;

Example: Computing prime numbers

For instance, we can declare an array of five ints like this: int numbers[5];

Exercises Software Development I. 06 Arrays. Declaration, Initialization, Usage // Multi-dimensional Arrays. November 14, 2012

Arrays. Chapter 7 (Done right after 4 arrays and loops go together, especially for loops)

CSED233: Data Structures (2018F) Lecture3: Arrays and Linked Lists

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

Mobile Computing Professor Pushpendra Singh Indraprastha Institute of Information Technology Delhi Java Basics Lecture 02

Lesson 06 Arrays. MIT 11053, Fundamentals of Programming By: S. Sabraz Nawaz Senior Lecturer in MIT Department of MIT FMC, SEUSL

Arrays & Linked Lists

CMSC131. Creating a Datatype Class Continued Exploration of Memory Model. Reminders

CS 230 Programming Languages

COSC 1P03. Chapter 1 Arrays. Introduction to Data Structures 1.2

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Lesson:9 Working with Array and String

First of all, it is a variable, just like other variables you studied

Arrays (Deitel chapter 7)

Unit 14. Passing Arrays & C++ Strings

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #44. Multidimensional Array and pointers

STRUCTURED DATA TYPE ARRAYS IN C++ ONE-DIMENSIONAL ARRAY TWO-DIMENSIONAL ARRAY

Arrays and Strings. Arash Rafiey. September 12, 2017

The Dynamic Typing Interlude

Gabriel Hugh Elkaim Spring CMPE 013/L: C Programming. CMPE 013/L: C Programming

Ar r ays and Pointer s

CMSC131 Final Exam Practice Questions

5.2 Defining and Initializing Arrays Statically in the Data Segment

Java Arrays. What could go wrong here? Motivation. Array Definition 9/28/18. Mohammad Ghafari

MITOCW watch?v=4dj1oguwtem

CT 229 Arrays Continued

Array. Prepared By - Rifat Shahriyar

Week 16: More on Collections and Immutability

Problem Solving through Programming In C Prof. Anupam Basu Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Array Basics: Outline

An array is a collection of data that holds fixed number of values of same type. It is also known as a set. An array is a data type.

Computer Programming: C++

AP Computer Science Chapter 10 Implementing and Using Classes Study Guide

Lesson 9: Introduction To Arrays (Updated for Java 1.5 Modifications by Mr. Dave Clausen)

Comp 11 Lectures. Mike Shah. June 26, Tufts University. Mike Shah (Tufts University) Comp 11 Lectures June 26, / 57

Java Programming. Computer Science 112

Java Programming. Computer Science 112

Object Oriented Programming: In this course we began an introduction to programming from an object-oriented approach.

(Refer Slide Time: 00:23)

+ Abstract Data Types

School of Computer Science CPS109 Course Notes Set 7 Alexander Ferworn Updated Fall 15 CPS109 Course Notes 7

CMSC131. Library Classes

Introduction to Object-Oriented Programming

11. Arrays. For example, an array containing 5 integer values of type int called foo could be represented as:

Types II. Hwansoo Han

Instructor: Eng.Omar Al-Nahal

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

Assignment 1: grid. Due November 20, 11:59 PM Introduction

Java How to Program, 10/e. Copyright by Pearson Education, Inc. All Rights Reserved.

Lecture 07, Fall 2018 Wednesday September 19

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Chapter 8 :: Composite Types

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #34. Function with pointer Argument

CMSC131. Inheritance. Object. When we talked about Object, I mentioned that all Java classes are "built" on top of that.

Slide 1 CS 170 Java Programming 1 Multidimensional Arrays Duration: 00:00:39 Advance mode: Auto

More non-primitive types Lesson 06

Hi everyone. I hope everyone had a good Fourth of July. Today we're going to be covering graph search. Now, whenever we bring up graph algorithms, we

CS 302: Introduction to Programming in Java. Lecture 9

Two Dimensional Array - An array with a multiple indexs.

A First Book of ANSI C Fourth Edition. Chapter 8 Arrays

Computers Programming Course 10. Iulian Năstac

Lab Session # 5 Arrays. ALQUDS University Department of Computer Engineering

CPSC 3740 Programming Languages University of Lethbridge. Data Types

Lecture (07) Arrays. By: Dr. Ahmed ElShafee. Dr. Ahmed ElShafee, ACU : Fall 2015, Programming I

How to declare an array in C?

6. Pointers, Structs, and Arrays. 1. Juli 2011

Computer Science & Engineering 150A Problem Solving Using Computers

Array Basics: Outline

6.001 Notes: Section 17.5

C# and Java. C# and Java are both modern object-oriented languages

Lecture 13 & 14. Single Dimensional Arrays. Dr. Martin O Connor CA166

Write a C program using arrays and structure

Introduction to Programming Using Java (98-388)

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

TOPICS TO COVER:-- Array declaration and use.

Arrays in Java Multi-dimensional Arrays

CHAPTER 7 ARRAYS: SETS OF SIMILAR DATA ITEMS

Transcription:

CMSC131 Data Structures: The Array Arrays: The Concept There are a wide variety of data structures that we can use or create to attempt to hold data in useful, organized, efficient ways. The MaritanPolynomial class you created as part of Project 4 is a simple data structure in some ways. It holds three values; one coefficient for each of the three terms. An array is a linear, contiguous, homogenous structure that can hold an arbitrary number of elements (as long as we know how many we want it to hold in advance).

Arrays: Some Properties Elements in an array of size n are numbered (some languages do 1..n, many like Java use 0..n-1). We can use those numbers to directly access an array position to read from or write into it. The data structure has a "first" and "last" position. In an array with more than 1 element, any position other than the first has a "previous" position and any position other than the last has a "next" position. We generally need to specify how many elements an array will have room for when we declare it. Arrays in Java In Java, an array is an object and we will have a variable serve as a reference to that object. We will indicate the type of elements that will be stored when we declare it. datatype[] arrayname; We will allocate the actual array. arrayname = new datatype[size]; We will access a position within an array by using the name of the reference variable, square brackets, with the position inside the brackets. arrayname[position]= value;

Some Motivation We have actually been using arrays quite a bit, they've just been behind the scenes. The String and StringBuffer classes both hold characters in an array. To better understand the efficiency example we saw, we need to consider how arrays are allocated and used. A Simple Problem What if I asked you to write a program that would allow the user to specify how many numbers they wanted to enter, then read them in, and print them out backwards?

What might we need to do? Declare some variables like an integer to store the answer when we ask the user how many numbers they want to enter and a variable to act as a reference to an array. Get the size request from the user. Allocate an array big enough to hold them all. Allow the user to enter those numbers one at a time and store them in the array. Traverse through the array backwards and print them out. What might we need to do? Declare some variables like an integer to int[] store dataarray; the answer when we ask the user how intmany numberofelements; numbers they want to enter and a variable to act as a reference to an array. Get the size request from the user. Allocate an array big enough to hold them all. Allow the user to enter those numbers one at a time and store them in the array. Traverse through the array backwards and print them out.

What might we need to do? Declare some variables like an integer to int[] store dataarray; the answer when we ask the user how intmany numberofelements; numbers they want to enter and a numberofelements variable to act as = a reference sc.nextint(); to an array. Get the size request from the user. Allocate an array big enough to hold them all. Allow the user to enter those numbers one at a time and store them in the array. Traverse through the array backwards and print them out. What might we need to do? Declare some variables like an integer to int[] store dataarray; the answer when we ask the user how intmany numberofelements; numbers they want to enter and a numberofelements variable to act as = a reference sc.nextint(); to an array. dataarray = new int[numberofelements]; Get the size request from the user. Allocate an array big enough to hold them all. Allow the user to enter those numbers one at a time and store them in the array. Traverse through the array backwards and print them out.

What might we need to do? Declare some variables like an integer to int[] store dataarray; the answer when we ask the user how intmany numberofelements; numbers they want to enter and a numberofelements variable to act as = a reference sc.nextint(); to an array. dataarray = new int[numberofelements]; Get the size request from the user. for Allocate (int i=0; an array i<numberofelements; big enough to hold them i++) all. { Allow dataarray[i] the user to enter = sc.nextint(); those numbers one at } //I am using i to make this fit page a time and store them in the array. Traverse through the array backwards and print them out. What might we need to do? Declare some variables like an integer to int[] store dataarray; the answer when we ask the user how intmany numberofelements; numbers they want to enter and a numberofelements variable to act as = a reference sc.nextint(); to an array. dataarray = new int[numberofelements]; Get the size request from the user. for Allocate (int i=0; an array i<numberofelements; big enough to hold them i++) all. { Allow dataarray[i] the user to enter = sc.nextint(); those numbers one at } //I am using i to make this fit page a time and store them in the array. for Traverse (int i=numberofelements-1; through the array backwards i>=0; and i--){ print System.out.print(dataArray[i] them + " "); } //I am using i to make this fit page

Length of an Array After allocating an array, we could have a variable that contains the size we requested. In some languages (including Java) an array will have an instance variable within the object that stores the size, but some other languages (like C++) do not, so we need to keep track of it. In Java, if you can access the size of the array via the instance variable length. arrayname.length The.length field is a read-only value, so you cannot grow an array by changing this number! Operations on an Array You can allocate and initialize an array at the same time if you know all of the information in advance. int[] firstfiveprimes = {2,3,5,7,11}; Remember that the variable we are using is a reference to an object. The following just makes a copy of the reference. int[] notcopied = firstfiveprimes; We'll look at three types of copies a little later in our discussion.

"Growing" an Array We can't really grow an array once it has been allocated. What we can do is allocate a new array, copy the information from the old one into the new one, and move the array reference to point to this new array object. Imagine we have an array named mydata and we want to make it twice as large int[] tempname = new int[mydata.length*2]; for (int i=0; i<mydata.length; i++){ tempname[i] = mydata[i]; Why StringBuffer was much faster! Every time we appended something onto our String, a whole new String object was created and the characters copied into it. With the StringBuffer, an initial size array is allocated, and only when that size is going to be exceeded by an operation is a new array allocated and all of the information copied in. In the current Java implementation, each time the size is going to be exceeded, the new array allocated is twice as large! What are the pros and cons of this?

Arrays of Objects Arrays elements can be references to objects as well. If so, the array will hold the references to those objects, not the actual objects. String[] cands = {"Obama", "Romney"}; StringBuffer[] cands = new StringBuffer[2]; cands[0] = new StringBuffer("Obama"); cands[1] = new StringBuffer("Romney"); String[] voters = new String[15000000]; Reference, Shallow, Deep Copies Reference Copy Student[] array1 = new Student[10]; //some code here which fills in the array with data Student[] array2 = array1; Shallow Copy Student[] array1 = new Student[10]; //some code here which fills in the array with data Student[] array2 = new

Reference, Shallow, Deep Copies Deep Copy Student[] array1 = new Student[10]; //some code here which fills in the array with data Student[] array2 = new Student[array1.length]; for (int i=0; i<array1.length; i++) { array2[i] = new Student(array1[i]); } //I am using i to make this fit page What is the danger in the above approach that neither reference nor shallow copies faced? To what "value" are the elements of an array of references automatically initialized by default in Java? 1. zero 2. null 3. it depends on the data type 4. they aren t initialized 60 0 of 5

Arrays class There is a useful library class Arrays which contains a variety of static methods. One subset of these that can be useful when exploring arrays is the group of tostring() methods which take an array and generates an ASCII visualization of that array. Another useful ability it provides is the ability to sort the contents of an array. This works for arrays of primitives and certain types of objects (we will see more of this later). Arrays as Arguments A reference to an array can be passed as an argument into a method. You do NOT specify the size of the array since the array itself isn't really being passed into the method, just the reference to it. Once the reference to the array is passed into a method, that method can access and alter the elements stored within the array. Let s look at ArrayParameter.java and ArrayParameterDriver.java

initarray1 1. 999999999 2. 012345678 3. Not Sure 0 of 5 initarray2 1. 999999999 2. 012345678 3. Not Sure 0 of 5

"Privacy" Issues Even without arrays, object references create certain "privacy" issues. Consider how references to objects work and what it means when a method returns a reference to an object. If it returns a reference to (for example) a private instance variable then the "outside world" now has direct access to it even though it is marked as private. This doesn't "really" matter for immutable objects, but for mutable ones it would allow code from outside of the class to alter the contents of private data. With arrays, even though the size of the array is immutable, the contents aren't. Multi-Dimensional Arrays It is sometimes useful to have a multidimensional structure for data storage. Consider representing things such as a chess board or storage warehouse. Declaring them as an "Array of Arrays" Allows us to have "ragged edged" arrays. Declaring them as a "xd Array" Doesn't give any real advantage in Java as far as I can tell but might depending on the language.

2D Arrays Syntax Examples Rectangular int[][] arr = new int[rows][cols]; Ragged int[][] arr = new int[rows][]; arr[0] = new int[colsinrow0]; arr[1] = new int[colsinrow1]; arr[2] = new int[colsinrow2]; arr[3] = new int[colsinrow3]; : : Upper Left-Hand Right Triangle We want to create a structure in which we store the distance from the "origin" to each cell in the structure, but only for the upper left-hand right triangle starting at that origin. Rectangular array or Ragged array? How do we make it work for a triangle with sides of length trianglesize?

Dealing with Ragged Arrays Generically int[][] reallyragged; reallyragged = new int[5][]; reallyragged[0] = new int[9]; reallyragged[1] = new int[3]; reallyragged[2] = new int[7]; reallyragged[3] = new int[0]; //NOTE reallyragged[4] = new int[12]; How could you go in and perform an operation on each element without having to hard-code the different lengths? Self-awareness Recall that arrays know their own length! counter = 0; for (row=0; row<reallyragged.length; row++) { for (col=0; col<reallyragged[row].length; col++) { reallyragged[row][col] = counter++; } } ----------------------------------------------- What would happen if we had used this? reallyragged[3] = null;

Copyright 2010-2012 : Evan Golub