Arrays Chapter 6 Chapter 6 1

Similar documents
Arrays. CS Feb 2008

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

Arrays. Chapter 6. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

Array Basics: Outline

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

More on Arrays CS 16: Solving Problems with Computers I Lecture #13

Introduction to Computer Science Midterm 3 Fall, Points

Chapter 6. Arrays. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

Arrays. Chapter 6. Array Basics Arrays in Classes and Methods Programming with Arrays and Classes Sorting Arrays Multidimensional Arrays

Module 5. Arrays. Adapted from Absolute Java, Rose Williams, Binghamton University

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

Array Basics: Outline

Chapter 9 Introduction to Arrays. Fundamentals of Java

Review of Important Topics in CS1600. Functions Arrays C-strings

Chapter 8 Arrays and Strings. Objectives. Objectives (cont d.) Introduction. Arrays 12/23/2016. In this chapter, you will:

Chapter 7 Multidimensional Arrays. Liang, Introduction to Java Programming, Tenth Edition, (c) 2013 Pearson Education, Inc. All rights reserved.

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

Arrays (Deitel chapter 7)

Arrays and Strings. Arash Rafiey. September 12, 2017

Computer Science & Engineering 150A Problem Solving Using Computers

Array Basics: Outline

CSE 1223: Introduction to Computer Programming in Java Chapter 6 Arrays

Arrays. Outline 1/7/2011. Arrays. Arrays are objects that help us organize large amounts of information. Chapter 7 focuses on:

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

Objectives. Chapter 8 Arrays and Strings. Objectives (cont d.) Introduction 12/14/2014. In this chapter, you will:

Chapter 7 Array. Array. C++, How to Program

Chapter 6: Arrays. Presentation slides for. Java Software Solutions. for AP* Computer Science 3rd Edition

Chapter 7 Multidimensional Arrays

11/19/2014. Arrays. Chapter 6: Arrays. Arrays. Arrays. Java Software Solutions for AP* Computer Science A 2nd Edition

C++ PROGRAMMING SKILLS Part 4: Arrays

&KDSWHU$UUD\VDQG9HFWRUV

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

Computer Programming: C++

Lab Assignment Three

Lecture 2 Arrays, Searching and Sorting (Arrays, multi-dimensional Arrays)

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

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

type arrayname [ size type arrayname [ size ] = { item ,, item size size

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

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

Array. Arrays. Declaring Arrays. Using Arrays

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

Chapter 8 Multi-Dimensional Arrays

Pointers. 1 Background. 1.1 Variables and Memory. 1.2 Motivating Pointers Massachusetts Institute of Technology

Array Basics: Outline

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

CSCE 110 PROGRAMMING FUNDAMENTALS. Prof. Amr Goneid AUC Part 7. 1-D & 2-D Arrays

ECE 122. Engineering Problem Solving with Java

ECE 122. Engineering Problem Solving with Java

Learning Objectives. Introduction to Arrays. Arrays in Functions. Programming with Arrays. Multidimensional Arrays

Administration. Objects and Arrays. Objects. Agenda. What is an Object? What is a Class?

COMP 110 Introduction to Programming. What did we discuss?

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

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

BITG 1113: Array (Part 1) LECTURE 8

Arrays and Lists CSC 121 Fall 2016 Howard Rosenthal

V2 3/5/2012. Programming in C. Introduction to Arrays. 111 Ch 07 A 1. Introduction to Arrays

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

Array. Prepared By - Rifat Shahriyar

C How to Program, 7/e by Pearson Education, Inc. All Rights Reserved.

Introduction to Object-Oriented Programming

Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved

More non-primitive types Lesson 06

York University AS/AK/ITEC INTRODUCTION TO DATA STRUCTURES. Midterm Sample I. Examiner: S. Chen Duration: One Hour and 30 Minutes

Exam 2. CSC 121 MW Class. Lecturer: Howard Rosenthal. April 26, 2017

Window Interfaces Using Swing. Chapter 12

C Arrays Pearson Education, Inc. All rights reserved.

15. Multidimensional Arrays

Principles of Programming. Chapter 6: Arrays

Instructor: Eng.Omar Al-Nahal

Example: Computing prime numbers

Arrays and Lists Review CSC 123 Fall 2018 Howard Rosenthal

Computer Science is...

BITG 1233: Array (Part 1) LECTURE 8 (Sem 2, 17/18)

Lecture Multidimensional Arrays and the ArrayList Class. Dr. Martin O Connor CA166

It is a constructor and is called using the new statement, for example, MyStuff m = new MyStuff();

Arrays and Lists CSC 121 Fall 2015 Howard Rosenthal

array Indexed same type

a) Answer all questions. b) Write your answers in the space provided. c) Show all calculations where applicable.

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS242 ARRAYS

Array Initialization

Multiple-Subscripted Arrays

Procedural Programming

Outline Introduction Arrays Declaring Arrays Examples Using Arrays Passing Arrays to Functions Sorting Arrays

COSC 236 Section 101 Computer Science 1 -- Prof. Michael A. Soderstrand

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

Lecture #8-10 Arrays

Arrays in Java Multi-dimensional Arrays

Object Oriented Programming. Java-Lecture 6 - Arrays

Java Bootcamp - Villanova University. CSC 2014 Java Bootcamp. Dr. Mary-Angela Papalaskari Department of Computing Sciences Villanova University

Chapter 12 Two-Dimensional Arrays

APS105. Collecting Elements 10/20/2013. Declaring an Array in C. How to collect elements of the same type? Arrays. General form: Example:

Arrays. Chapter 7. Walter Savitch Frank M. Carrano

Defining Classes and Methods

Arrays and functions Multidimensional arrays Sorting and algorithm efficiency

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

Introduction to Programming Using Java (98-388)

Maltepe University Computer Engineering Department. BİL 133 Algoritma ve Programlama. Chapter 8: Arrays and pointers

Dynamic Data Structures and Generics. Chapter 10

CS 230 Programming Languages

Transcription:

Arrays 1

Reminders Project 4 released: due Oct 6 @ 10:30 pm Project 1 regrades due by midnight tonight Project 2 grades posted on WebCT regrade requests due by Friday Oct 7 midnight (send to rnehme@cs.purdue.edu) 2

Exam 1 Info Exam will be returned in recitation today Solution available on the website Scores are available on WebCT Problems/scoring errors: talk to your GTA Exam 1 Stats: Mean: 73.2 Median: 77 High: 98 3

Creating and Accessing Arrays example double[] temperature = new double[7]; is like declaring seven variables of type double, named temperature[0], temperature[1], temperature [2], temperature[3], temperature[4], temperature[5], temperature[6]. Similar to: Automobile mycar = new Automobile( Toyota ); 4

Creating and Accessing Arrays, cont. These variables can be used just like any other variables of type double. examples temperature[3] = 32.0; temperature[6] = temperature[3] + 5; System.out.println(temperature[6]); temperature[index] = 66.5; These variables are called indexed variables, elements, or subscripted variables. 5

The length Instance Variable An array has only one public instance variable, length. The length variable stores the number of elements the array can hold. Using Array_Name.length typically produces clearer code than using an integer literal. length is an instance variable, not a method Array_Name.length() wont work 6

7

Indices and length The indices of an array start with 0 and end with Array_Name.length-1. When a for loop is used to step through an array, the loop control variable should start at 0 and end at length-1. example for (lcv = 0; lcv < temperature.length; lcv++) 8

Array Index Out of Bounds Every index must evaluate to an integer which is not less than 0 and not greater than Array_Name.length-1. Otherwise, the index is said to be out of bounds or invalid. An out-of-bounds index will produce a runtime error. 9

Arrays of Objects 10

Entire Arrays as Method Arguments An entire array can be used as a single argument passed to a method. example double[] a = new double[10]; SampleClass.change(a);... public static void change(double[] d) No brackets accompany the argument. Method change accepts an array of any size. 11

Methods that Return Arrays A method can return an array. The mechanism is basically the same as for any other returned type. example public static Base_Type[] Method_Name (Parameter_List) Base_Type Array_Name; return Array_Name; The method need not be public or static. 12

13

Sorting Arrays Sometime we want numbers in an array sorted from smallest to largest, or from largest to smallest. Sometimes we want the strings referenced by an array to be in alphabetical order. Sorting techniques typically are easy to adapt to sort any type that can be ordered. 14

Selection Sort The selection sort arranges the values in an an array so that a[0] <= a[1] <= a[2] <= a[a.length-1] The selection sort places the smallest item in a[0], the next smallest item in a[1], and so on for all but the last item. for(i = 0; i <a.length-1; i++) place the ith smallest item in a[i] 15

Selection Sort, cont. Selection sort begins by finding the smallest item in the array and swapping it with the item in a[0]. Selection sort continues by finding the smallest item in the remainder of the array and swapping it with the next item in array a. Selection sort terminates when only one item remains. 16

Selection Sort, cont. 17

18

Swapping Elements To swap two elements a[i] and a[j], one of them must be saved temporarily. 19

Swapping Elements, cont. class interchange 20

Introduction to Multidimensional Arrays An array with more than one index sometimes is useful. example: savings account balances at various interest rates for various numbers of years columns for interest rates rows for years This two-dimensional table calls for a twodimensional array. 21

Introduction to Multidimensional Arrays, cont. 22

Introduction to Multidimensional Arrays, cont. 23

Introduction to Multidimensional Arrays, cont. An array with n indices is called an ndimensional array. The arrays we considered previously, which had one index, were one-dimensional arrays. 24

Multidimensional-Array Basics example declaration int[][] table = new int [10][6]; or int[][] table; table = new int[10][6]; The number of bracket pairs in the declaration is the same as the number of indices. 25

Multidimensional-Array Basics, cont. syntax Base_Type[] [] Array_Name = new Base_Type[Length_1] [Length_n]; examples char[][] page = new char [100][80]; double[][][] threedpicture = new double[10][20][30]; SomeClass[][] entry = new SomeClass[100][80]; 26

Multidimensional-Array Basics, cont. Nested for loops can be used to change the values of the elements of the array and to display the values of the elements of the array. 27

28

Multidimensional-Array Parameters Methods may have multidimensional-array parameters. 29

30

Multidimensional-Array Returned Values A method may return a multidimensional array. example public static double[][] corner(double[][] sarray, int i) { double[][] temp = new double[i][i]; return temp; } 31

Implementation of Multidimensional Arrays Multidimensional arrays are implemented in Java using one-dimensional arrays. Consider int[][] table = new int[10][6]; The array table is a one-dimensional array of length 10. Its base type is int[]. Therefore, it is an array of arrays. 32

Implementation of Multidimensional Arrays, cont. This permits us to use the length instance variable, instead of an integer literal, to control a for loop used to initialize or print the values of the elements of an array. example for (r = 0; r < table.length; r++) for (c = 0; c < table[r].length; c++) 33

Ragged Arrays Since a two-dimensional array in Java is an array of arrays, each row can have a different number of elements (columns). Arrays in which rows have different numbers of elements are called ragged arrays. 34

Ragged Arrays, cont. Example int[][] b = new int[3][]; b[0] = new int[5]; b[1] = new int[7]; b[2] = new int[4]; 35

(optional) Graphics Supplement: Outline Part 1: Text Areas and Text Fields Part 2: Drawing Polygons and Polylines 36

Text Areas and Text Fields A text area is a window that can be used for text input and text output. any number of lines any number of characters per line A text field allows only one line of characters. Text area and text fields provide areas for changeable text in a GUI. 37

Programming Example: A Question and Answer Applet The applet accepts a question, requests some advice, and provides an answer. The applet contains some advice for the first user, but then takes the advice provided by one user, and uses it to answer the next user. 38

39

Programming Example:, cont. 40

JTextArea Objects A text area is an object of the class A JTextArea can be created with JTextArea. private JTextArea thetext; thetext = new JTextArea(LINES, CHAR_PER_LINE); Typically, this occurs inside the init method. 41

JTextArea Objects, cont. The text in the text area can be read using String question = thetext.gettext(); The text in the text area can be set using thetext.settext( That s difficult.\n + I need some advice.\n + Give me some advice and click. ); Text can be entered in excess of the specified size, but may not be entirely visible. 42

JTextField Objects A text field is an object of the class A JTextField can be created with JTextField. private JTextField thetext; thetext = new JTextField(NUMBER_OF_CHARS); Typically, this occurs inside the init method. 43

JTextField Objects, cont. The text in the text field can be read using String question = thetext.gettext(); The text in the text area can be set using thetext.settext( That s all, folks. ); Text can be entered in excess of the specified size, but may not be entirely visible. 44

Drawing Polygons and Polylines The methods drawpolygon and fillpolygon allow you to draw polygons which are closed figures made of of line segments that meet at vertices but do not cross. A polyline is similar to a polygon, but uses the method drawpolyline and need not be closed. 45

Drawing Polygons and Polylines, cont. 46

Drawing Polygons and Polylines, cont. class House 47

Drawing Polygons and Polylines, cont. 48

Method drawpolygon syntax canvas.drawpolygon(array_of_xs, Array_of_ys, Number_of_Points); example private int[] xcoord = {150, 150, 200, 250, 250}; private int[] ycoord = {100, 40, 20, 40, 100}; canvas.drawpolygon(xcoord, ycoord, xcoord.length); 49

Method fillpolygon syntax canvas.fillpolygon(array_of_xs, Array_of_ys, Number_of_Points); example private int[] xcoord = {150, 150, 200, 250, 250}; private int[] ycoord = {100, 40, 20, 40, 100}; canvas.fillpolygon(xcoord, ycoord, xcoord.length); 50

Method drawpolyline syntax canvas.drawpolyline(array_of_xs, Array_of_ys, Number_of_Points); example private int[] xcoord = {150, 150, 200, 250, 250}; private int[] ycoord = {100, 40, 20, 40, 100}; canvas.drawpolyline(xcoord, ycoord, xcoord.length); 51

Method drawpolyline, cont. There is no automatic line segment from the last point to the first point. As a consequence, the figure typically is not closed. A polyline cannot be filled. 52

Summary You have learned about arrays and how to use them in Java programs. You have learned how to use array parameters and how to define methods that return an array. You have learned the proper use of an array as an instance variable. You have learned about multidimensional arrays. 53

Summary, cont. (optional) You have learned about text fields and text areas in applets. (optional) You have learned to draw arbitrary polygons and polylines in applets. 54