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

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

Array Basics: Outline

Array Basics: Outline

Array Basics: Outline

Arrays. Chapter 7. Walter Savitch Frank M. Carrano

Array Basics: Outline

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

Arrays. Fundamentals of Computer Science

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

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

Defining Classes and Methods

Computer Programming: C++

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

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

Chapter 6 SINGLE-DIMENSIONAL ARRAYS

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

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

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

A PROGRAM IS A SEQUENCE of instructions that a computer can execute to

Array. Prepared By - Rifat Shahriyar

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

COMP 110 Introduction to Programming. What did we discuss?

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

Tutorial 13 Salary Survey Application: Introducing One- Dimensional Arrays

1 Lexical Considerations

Wentworth Institute of Technology. Engineering & Technology WIT COMP1000. Arrays

1 Short Answer (15 Points Each)

Methods and Data (Savitch, Chapter 5)

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

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

( &% class MyClass { }

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

The University Of Michigan. EECS402 Lecture 05. Andrew M. Morgan. Savitch Ch. 5 Arrays Multi-Dimensional Arrays. Consider This Program

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

Personal SE. Functions & Arrays

Software and Programming 1

Defining Classes and Methods

St. Edmund Preparatory High School Brooklyn, NY

TOPICS TO COVER:-- Array declaration and use.

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

9. Arrays. Compound Data Types: type name [elements]; int billy [5];

Chapter 9 Introduction to Arrays. Fundamentals of Java

Computer Science & Engineering 150A Problem Solving Using Computers

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

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

CS/IT 114 Introduction to Java, Part 1 FALL 2016 CLASS 10: OCT. 6TH INSTRUCTOR: JIAYIN WANG

Lecture 17. Instructor: Craig Duckett. Passing & Returning Arrays

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

2. Each element of an array is accessed by a number known as a(n) a. a. subscript b. size declarator c. address d. specifier

Array Elements as Function Parameters

Arrays. Lecture 11 CGS 3416 Fall October 26, 2015

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

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

Lexical Considerations

All answers will be posted on web site, and most will be reviewed in class.

CS1150 Principles of Computer Science Methods

APCS Semester #1 Final Exam Practice Problems

Object oriented programming C++

This page intentionally left blank

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

Defining Classes and Methods

Arrays (Deitel chapter 7)

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

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

CS111: PROGRAMMING LANGUAGE II

Example: Computing prime numbers

Programming with Java

Lexical Considerations

Method OverLoading printf method Arrays Declaring and Using Arrays Arrays of Objects Array as Parameters

Every language has its own scoping rules. For example, what is the scope of variable j in this Java program?

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):

Defining Classes and Methods. Objectives. Objectives 6/27/2014. Chapter 5

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

The Warhol Language Reference Manual

Instructor: Eng.Omar Al-Nahal

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

COS 126 Midterm 1 Written Exam Fall 2011

Last Class. While loops Infinite loops Loop counters Iterations

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

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

C++ For Science and Engineering Lecture 12

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

Java Bytecode (binary file)

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

EXERCISES SOFTWARE DEVELOPMENT I. 04 Arrays & Methods 2018W

Object oriented programming. Instructor: Masoud Asghari Web page: Ch: 3

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

Array Initialization

HST 952. Computing for Biomedical Scientists Lecture 5

Arrays. Eng. Mohammed Abdualal

UEE1302 (1102) F10 Introduction to Computers and Programming (I)

EECS168 Exam 3 Review

We have written lots of code so far It has all been inside of the main() method What about a big program? The main() method is going to get really

Mandatory Exercise 1 INF3110

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

UNIT - I. Introduction to C Programming. BY A. Vijay Bharath

Data dependent execution order data dependent control flow

Operational Semantics. One-Slide Summary. Lecture Outline

Arrays Chapter 6 Chapter 6 1

Computer Science 1 Honors

Transcription:

Array Basics: Outline 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 Creating and Accessing Arrays Array Details The Instance Variable length More About Array Indices Analyzing Arrays 2 Creating and Accessing Arrays Creating and Accessing Arrays An array is a special kind of object Think of as collection of variables of same type Creating an array with 7 variables of type double 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 3 4

Creating and Accessing Arrays Array Details Syntax for declaring an array with new Sample screen output The number of elements in an array is its length The type of the array elements is the array's base type 5 6 Square Brackets with Arrays Array Details With a data type when declaring an array: int [] pressure; <- Java convention int pressure []; <- C/C++ convention 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(); Figure 7.2 Array terminology 7 8

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 The Instance Variable length Sample screen output 9 10 More About Array Indices Initializing Arrays 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 time exception occurs Get used to using index 0 Possible to initialize at declaration time Also may use normal assignment statements One at a time In a loop 11 12

Arrays in Classes and Methods: Outline Indexed Variables as Method Arguments Entire 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 Example 13 14 Entire Arrays as Arguments Entire Arrays as Arguments Declaration of array parameter similar to how an array is declared Example: 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 entire array, do not use square brackets in the argument. Kind of like you only pass the variable name of a String or an integer, as well. 15 16

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 multiple 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 Example 17 18 Array Assignment and Equality Note results of == Note definition and use of the static method equals from the Arrays class Receives two array parameters Checks length and each individual pair of array elements Remember array types are reference types Methods that Return Arrays A Java method may return an array Note definition of return type as an array To return the array value Declare a local array Use that identifier in the return statement Example 19 20

Programming with Arrays and Classes: Outline Programming Example: A Specialized List Class Partially Filled Arrays 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 entire list, start with blank list But no method to modify or delete list item Maximum number of items can be specified 21 22 Partially Filled Arrays Array size specified at definition Not all elements of the array might receive values This is termed a partially filled array Programmer must keep track of how much of array is used 23