Array Basics: Outline

Similar documents
Array Basics: Outline

Array Basics: Outline

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

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

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

Arrays. Chapter 7. Walter Savitch Frank M. Carrano

Array Basics: Outline

Arrays. Fundamentals of Computer Science

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

method method public class Temperature { public static void main(string[] args) { // your code here } new

Learning objec<ves. Classes, Objects, and Methods. Problem Decomposi<on. Problem Decomposi<on (cont d)

Declaring and ini,alizing 2D arrays

Algorithms for Arrays Vectors Pointers CS 16: Solving Problems with Computers I Lecture #14

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

Methods. Methods. Mysteries Revealed

Arrays. Comp Sci 1570 Introduction to C++ Array basics. arrays. Arrays as parameters to functions. Sorting arrays. Random stuff

Lecture 07, Fall 2018 Wednesday September 19

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

High Institute of Computer Science & Information Technology Term : 1 st. El-Shorouk Academy Acad. Year : 2013 / Year : 2 nd

Arrays 2 CS 16: Solving Problems with Computers I Lecture #12

Computer Programming: C++

Class C{ int a; } what variables below are objects : a. C c; b. String str; c. Scanner scanner; d. int num; e. float f;

Array. Prepared By - Rifat Shahriyar

More on variables, arrays, debugging

CS1150 Principles of Computer Science Methods

APCS Semester #1 Final Exam Practice Problems

Thinking Induc,vely. COS 326 David Walker Princeton University

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

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

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

Defining Classes and Methods

CS 152: Data Structures with Java Hello World with the IntelliJ IDE

Mo#va#ng the OO Way. COMP 401, Fall 2017 Lecture 05

CS1150 Principles of Computer Science Methods

Computer Science & Engineering 150A Problem Solving Using Computers

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

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

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

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 8: Abstract Data Types

Data Base Management System LAB LECTURES

COSC 111: Computer Programming I. Dr. Bowen Hui University of Bri>sh Columbia Okanagan

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

Lab Midterms Not Graded Yet

Give one example where you might wish to use a three dimensional array

Introduction to Object-Oriented Programming

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

BBM 102 Introduc0on to Programming II Spring 2014

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

1 Lexical Considerations

Related Course Objec6ves

More on Strings in C++ Arrays CS 16: Solving Problems with Computers I Lecture #11

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

Chapter 7: Arrays CS 121. April 9, Department of Computer Science College of Engineering Boise State University. Chapter 7: Arrays CS / 41

Arrays. Defining arrays, declaration and initialization of arrays. Designed by Parul Khurana, LIECA.

Vectors and Pointers CS 16: Solving Problems with Computers I Lecture #13

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

HST 952. Computing for Biomedical Scientists Lecture 5

Q1: Functions / 33 Q2: Arrays / 47 Q3: Multiple choice / 20 TOTAL SCORE / 100 Q4: EXTRA CREDIT / 10

Arrays Chapter 6 Chapter 6 1

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

Java. Package, Interface & Excep2on

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

Introduction to Internet of Things Prof. Sudip Misra Department of Computer Science & Engineering Indian Institute of Technology, Kharagpur

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

D0010E Object- oriented programming and design. Today. Today An introduc<on to the basic syntax and seman<cs of Java

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

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

Overview. A mathema5cal proof technique Proves statements about natural numbers 0,1,2,... (or more generally, induc+vely defined objects) Merge Sort

Chapter 12 Two-Dimensional Arrays

1 Short Answer (15 Points Each)

CS 139 Practice Midterm Questions #2

Strings, Arrays, A1. COMP 401, Spring 2015 Lecture 4 1/20/2015

Arrays. COMS W1007 Introduction to Computer Science. Christopher Conway 10 June 2003

Instructions. This exam has 7 questions, worth 10 points each. You have 50 minutes.

CS-201 Introduction to Programming with Java

Java Bytecode (binary file)

Introduction to Java. Handout-1d. cs402 - Spring

array Indexed same type

Programming Languages and Techniques (CIS120)

Chapter 9 Introduction to Arrays. Fundamentals of Java

Methods and Data (Savitch, Chapter 5)

Learning objec-ves. Declaring and ini-alizing 2D arrays. Prin-ng 2D arrays. Using 2D arrays Decomposi-on of a solu-on into objects and methods

Arrays. CS Feb 2008

Arrays. Loops + arrays: challenge problem. Arrays (II) An array is a set of variables of the same type accessed by their index.

Object Oriented Programming. Java-Lecture 6 - Arrays

Objectives. Defining Classes and Methods. Objectives. Class and Method Definitions: Outline 7/13/09

TOPICS TO COVER:-- Array declaration and use.

G - GCE SOLUTIONS. Siddharth Kumar, Principal Programmer. Add Derived Parameters using Multi-Dimensional Arrays. Derive value from excellence

CS111: PROGRAMMING LANGUAGE II

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

CSS 161 Fundamentals of Compu3ng. Assignments, Expressions, Operators, Console input & output October 1, 2012

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

CP122 CS I. Iteration

Programming with Arrays Intro to Pointers CS 16: Solving Problems with Computers I Lecture #11

Common Loop Algorithms 9/21/16 42

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

Loops! Step- by- step. An Example while Loop. Flow of Control: Loops (Savitch, Chapter 4)

Review Chapter 6 in Bravaco. Short Answers 1. This type of method does not return a value. a. null b. void c. empty d. anonymous

Data Structures in Memory!

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

Transcription:

Array Basics: Outline More Arrays (Savitch, Chapter 7) TOPICS Array Basics Arrays in Classes and Methods Programming with Arrays Searching and Sorting Arrays Multi-Dimensional Arrays Static Variables and Constants Crea%ng and Accessing Arrays Array Details The Instance Variable length More About Array Indices Analyzing Arrays CS160 - Spring Semester 2016 2 Crea%ng and Accessing Arrays An array is a special kind of object Think of as collec%on of variables of same type Crea%ng an array with 7 variables of type double Crea%ng and Accessing Arrays Figure 7.1 A common way to visualize an array To access an element use The name of the array An index number enclosed in braces Array indices begin at zero Note class ArrayOfTemperatures CS160 - Spring Semester 2016 3 CS160 - Spring Semester 2016 4 1

Crea%ng and Accessing Arrays Array Details Syntax for declaring an array with new The number of elements in an array is its length The type of the array elements is the array's base type CS160 - Spring Semester 2016 5 CS160 - Spring Semester 2016 6 Square Brackets with Arrays With a data type when declaring an array: int []pressure; int pressure[]; To enclose an integer expression to declare the length of the array: pressure = new int[100]; To access the array with an index value: pressure[3] = keyboard.nextint(); Array Details Figure 7.2 Array terminology CS160 - Spring Semester 2016 7 CS160 - Spring Semester 2016 8 2

The Instance Variable length As an object an array has only one public instance variable: Variable length Contains number of elements in the array It is final, value cannot be changed Note class ArrayOfTemperatures2 The Instance Variable length CS160 - Spring Semester 2016 9 CS160 - Spring Semester 2016 10 More About Array Indices Index of first array element is 0 Last valid Index is arrayname.length 1 Array indices must be within bounds to be valid: When program tries to access outside bounds, run %me excep%on occurs Get used to using index 0 Ini%alizing Arrays Possible to ini%alize at declara%on %me Also may use normal assignment statements One at a %me In a loop CS160 - Spring Semester 2016 11 CS160 - Spring Semester 2016 12 3

Arrays in Classes and Methods: Outline Indexed Variables as Method Arguments En%re Arrays as Arguments to a Method Arguments for the Method main Array Assignment and Equality Methods that Return Arrays Indexed Variables as Method Arguments Indexed variable of an array Example a[i] Can be used anywhere variable of array base type can be used View class ArgumentDemo using indexed variable as an argument CS160 - Spring Semester 2016 13 CS160 - Spring Semester 2016 14 En%re Arrays as Arguments Declara%on of array parameter similar to how an array is declared Example: En%re Arrays as Arguments Array parameter in a method heading does not specify the length An array of any length can be passed to the method Inside the method, elements of the array can be changed When you pass the en%re array, do not use square brackets in the actual parameter CS160 - Spring Semester 2016 15 CS160 - Spring Semester 2016 16 4

Arguments for Method main Recall heading of method main public static void main (String[] args) This declares an array Formal parameter named args Its base type is String Thus possible to pass to the run of a program mul%ple strings These can then be used by the program Array Assignment and Equality Arrays are objects Assignment and equality operators behave (misbehave) as specified in previous chapter Variable for the array object contains memory address of the object Assignment operator = copies this address Equality operator == tests whether two arrays are stored in same place in memory CS160 - Spring Semester 2016 17 CS160 - Spring Semester 2016 18 Array Assignment and Equality Array Assignment and Equality Two kinds of equality View class TestEquals Note results of == Note defini%on and use of method equals Receives two array parameters Checks length and each individual pair of array elements Remember array types are reference types CS160 - Spring Semester 2016 19 CS160 - Spring Semester 2016 20 5

Methods that Return Arrays A Java method may return an array View class ReturnArrayDemo Note defini%on of return type as an array To return the array value Declare a local array Use that iden%fier in the return statement Programming with Arrays and Classes: Outline Programming Example: A Specialized List Class Par%ally Filled Arrays CS160 - Spring Semester 2016 21 CS160 - Spring Semester 2016 22 Programming Example A specialized List class Objects can be used for keeping lists of items Methods include Capability to add items to the list Also delete en%re list, start with blank list But no method to modify or delete list item Maximum number of items can be specified Programming Example View class ListDemo Note declara%on of the list object Note method calls CS160 - Spring Semester 2016 23 CS160 - Spring Semester 2016 24 6

Programming Example Programming Example Now view array wrapped in a class to represent a list, class OneWayNoRepeatsList Notable code elements Declara%on of private array Method to find n th list item Method to check if item is on the list or not CS160 - Spring Semester 2016 25 CS160 - Spring Semester 2016 26 Par%ally Filled Arrays Array size specified at defini%on Not all elements of the array might receive values This is termed a par$ally filled array Programmer must keep track of how much of array is used Par%ally Filled Arrays Figure 7.4 A par%ally filled array CS160 - Spring Semester 2016 27 CS160 - Spring Semester 2016 28 7

Mul%dimensional- Array Basics Consider Figure 7.6, a table of values Mul%dimensional- Array Basics Figure 7.7 Row and column indices for an array named table table[3][2] has a value of 1262 CS160 - Spring Semester 2016 29 CS160 - Spring Semester 2016 30 Mul%dimensional- Array Basics Mul%dimensional- Array Basics We can access elements of the table with a nested for loop Example: View class InterestTable CS160 - Spring Semester 2016 31 CS160 - Spring Semester 2016 32 8

Java's Representa%on of Mul%dimensional Arrays Mul%dimensional array represented as several one- dimensional arrays Given int [][] table = new int [10][6]; Array table is actually 1 dimensional of type int[] It is an array of arrays Important when sequencing through mul%dimensional array Summary An array is a collec%on of variables all of the same type Arrays are objects, created with operator new Elements numbered star%ng with 0, ending with 1 less than length Indexed variable can be used as a parameter treated like variable of base type CS160 - Spring Semester 2016 33 CS160 - Spring Semester 2016 34 Summary Summary En%re array can be passed as parameter to a method Method return value can be an array Par%ally filled array usually stores values in ini%al segment, use an int to track how many are used Mul%dimensional arrays are implemented as an array of arrays Treat two- dimensional array as a table with rows and columns CS160 - Spring Semester 2016 35 CS160 - Spring Semester 2016 36 9