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

Similar documents
Chapter 7: Arrays and the ArrayList Class

Arrays and the TOPICS CHAPTER 8. CONCEPT: An array can hold multiple values of the same data type simultaneously.

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

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

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

TOPICS TO COVER:-- Array declaration and use.

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

Chapter 9 Introduction to Arrays. Fundamentals of Java

ECE 122. Engineering Problem Solving with Java

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

Multiple-Subscripted Arrays

Arrays (Deitel chapter 7)

APCS Semester #1 Final Exam Practice Problems

This page intentionally left blank

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

Array. Prepared By - Rifat Shahriyar

Introduction to Computer Science Midterm 3 Fall, Points

Angela Z: A Language that facilitate the Matrix wise operations Language Reference Manual

Chapter 6: Arrays. Starting Out with Games and Graphics in C++ Second Edition. by Tony Gaddis

calling a function - function-name(argument list); y = square ( z ); include parentheses even if parameter list is empty!

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

Weiss Chapter 1 terminology (parenthesized numbers are page numbers)

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

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

Array Basics: Outline

Chapter 2: Functions and Control Structures

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

The Warhol Language Reference Manual

1 Lexical Considerations

Programming for Engineers Arrays

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

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Programming with Java

Crayon (.cry) Language Reference Manual. Naman Agrawal (na2603) Vaidehi Dalmia (vd2302) Ganesh Ravichandran (gr2483) David Smart (ds3361)

Lexical Considerations

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

CONTENTS. PART 1 Structured Programming 1. 1 Getting started 3. 2 Basic programming elements 17

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. A single variable can only hold one value. Declared using [] operator:


Spring 2010 Java Programming

Lexical Considerations

Module 6: Array in C

Chapter 7 : Arrays (pp )

Chapter 2 Using Data. Instructor s Manual Table of Contents. At a Glance. Overview. Objectives. Teaching Tips. Quick Quizzes. Class Discussion Topics

STUDENT LESSON A12 Iterations

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

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

CSc Introduction to Computing

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

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

More non-primitive types Lesson 06

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

C++ Programming. Arrays and Vectors. Chapter 6. Objectives. Chiou. This chapter introduces the important topic of data structures collections

Full file at

Lecture 3 Tao Wang 1

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.

Array Basics: Outline

MyProgram m i ng Lab. get with the programming. Through the power of practice and immediate personalized

WEEK 4 OPERATORS, EXPRESSIONS AND STATEMENTS

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

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

GridLang: Grid Based Game Development Language Language Reference Manual. Programming Language and Translators - Spring 2017 Prof.

1 Introduction Java, the beginning Java Virtual Machine A First Program BlueJ Raspberry Pi...

Language Reference Manual simplicity

Lecture 17. For Array Class Shenanigans

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

Java Bytecode (binary file)

Object Oriented Programming. Java-Lecture 6 - Arrays

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

Chapter Nine Arrays. Sixth Edition

Problem Solving With Loops

Full file at

FORM 2 (Please put your name and form # on the scantron!!!!)

Java Primer 1: Types, Classes and Operators

H212 Introduction to Software Systems Honors

Week 4 Lecture 1. Expressions and Functions

Session 8.2. Finding Winners Using Arrays

Java is an objet-oriented programming language providing features that support

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

Chapter 3: Operators, Expressions and Type Conversion

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

Arrays and Collections. C# Programming: From Problem Analysis to Program Design 2nd Edition. David McDonald, Ph.D. Director of Emerging Technologies

AP Computer Science Lists The Array type

Arrays and ArrayLists. David Greenstein Monta Vista High School

COMP Primitive and Class Types. Yi Hong May 14, 2015

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

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

VARIABLES AND TYPES CITS1001

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

Array Basics: Outline

Lecture 2 Tao Wang 1

First Name: Last: ID# 1. Hexadecimal uses the symbols 1, 2, 3, 4, 5, 6, 7 8, 9, A, B, C, D, E, F,G.

Chapter 12 Two-Dimensional Arrays

There are four numeric types: 1. Integers, represented as a 32 bit (or longer) quantity. Digits sequences (possibly) signed are integer literals:

CS110: PROGRAMMING LANGUAGE I

How to declare an array in C?

Introduction to Programming Using Java (98-388)

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

Last Class. While loops Infinite loops Loop counters Iterations

Transcription:

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

Starting Out with Java: From Control Structures Through Objects Sixth Edition Chapter 7 Arrays

Chapter Topics (1 of 2) 7.1 Introduction to Arrays 7.2 Processing Array Contents 7.3 Passing Arrays as Arguments to Methods 7.4 Some Useful Array Algorithms and Operations 7.5 Returning Arrays from Methods 7.6 String Arrays 7.7 Arrays of Objects

Chapter Topics (2 of 2) 7.8 The Sequential Search Algorithm 7.9 Parallel Arrays 7.10 Two-Dimensional Arrays 7.11 Arrays with Three or More Dimensions 7.12 The Selection Sort and the Binary Search 7.13 Command-Line Arguments 7.14 The ArrayList Class

7.1 Introduction to Arrays Primitive variables are designed to hold only one value at a time. Arrays allow us to create a collection of like values that are indexed. An array can store any type of data but only one type of data at a time. An array is a list of data elements.

Creating Arrays (1 of 3) An array is an object so it needs an object reference. The next step creates the array and assigns its address to the numbers variable.

Creating Arrays (2 of 3) It is possible to declare an array reference and create it in the same statement. Arrays may be of any type.

Creating Arrays (3 of 3) The array size must be a non-negative number. It may be a literal value, a constant, or variable. Once created, an array size is fixed and cannot be changed.

Accessing the Elements of an Array An array is accessed by: the reference name a subscript that identifies which element in the array to access.

Inputting and Outputting Array Elements Array elements can be treated as any other variable. They are simply accessed by the same name and a subscript. See example: ArrayDemo1.java Array subscripts can be accessed using variables (such as for loop counters). See example: ArrayDemo2.java

Bounds Checking Array indexes always start at zero and continue to (array length 1). int [] values = new int[10]; This array would have indexes 0 through 9. See example: InvalidSubscript.java In for loops, it is typical to use i, j, and k as counting variables. It might help to think of i as representing the word index.

Off-By-One Errors It is very easy to be off-by-one when accessing arrays. Here, the equal sign allows the loop to continue on to index 100, where 99 is the last index in the array. This code would throw an ArrayIndexOutOfBoundsException.

Array Initialization When relatively few items need to be initialized, an initialization list can be used to initialize the array. The numbers in the list are stored in the array in order: days[0] is assigned 31, days[1] is assigned 28, days[2] is assigned 31, days[3] is assigned 30, etc. See example: ArrayInitialization.java

Alternate Array Declaration Previously we showed arrays being declared: However, the brackets can also go here: These are equivalent but the first style is typical. Multiple arrays can be declared on the same line. With the alternate notation each variable must have brackets. The scores variable in this instance is simply an int variable.

7.2 Processing Array Contents (1 of 2) Processing data in an array is the same as any other variable. Pre and post increment works the same: See example: PayArray.java

7.2 Processing Array Contents (2 of 2) Array elements can be used in relational operations: They can be used as loop conditions:

Array Length Arrays are objects and provide a public field named length that is a constant that can be tested. The length of this array is 25. The length of an array can be obtained via its length constant. The variable size will contain 25.

The Enhanced for Loop (1 of 2) Simplified array processing (read only) Always goes through all elements General format:

The Enhanced for Loop (2 of 2) Example:

Array Size (1 of 2) The length constant can be used in a loop to provide automatic bounding.

Array Size (2 of 2) You can let the user specify the size of an array: See example: DisplayTestScores.java

Reassigning Array References (1 of 3) An array reference can be assigned to another array of the same type. If the first (10 element) array no longer has a reference to it, it will be garbage collected.

Reassigning Array References (2 of 3)

Reassigning Array References (3 of 3)

Copying Arrays (1 of 2) This is not the way to copy an array. Example: SameArray.java

Copying Arrays (2 of 2) You cannot copy an array by merely assigning one reference variable to another. You need to copy the individual elements of one array to another. This code copies each element of firstarray to the corresponding element of secondarray.

Comparing Arrays The == operator determines only whether array references point to the same array object.

Comparing Arrays: Example

Useful Array Operations (1 of 2) Finding the Highest Value Finding the Lowest Value

Useful Array Operations (2 of 2) Summing Array Elements: Averaging Array Elements: Example: SalesData.java, Sales.java

Passing Array Elements to a Method When a single element of an array is passed to a method it is handled like any other variable. See example: PassElements.java More often you will want to write methods to process array data by passing the entire array, not just one element at a time.

Passing Arrays as Arguments Arrays are objects. Their references can be passed to methods like any other object reference variable. Example: PassArray.java

Returning an Array Reference A method can return a reference to an array. The return type of the method must be declared as an array of the right type. The getarray method is a public static method that returns an array of doubles. See example: ReturnArray.java

7.6 String Arrays (1 of 3) Arrays are not limited to primitive data. An array of String objects can be created: Example: MonthDays.java

7.6 String Arrays (2 of 3) If an initialization list is not provided, the new keyword must be used to create the array:

7.6 String Arrays (3 of 3) When an array is created in this manner, each element of the array must be initialized.

The length Field & the length Method Arrays have a final field named length. String objects have a method named length. To display the length of each string held in a String array: An array s length is a field You do not write a set of parentheses after its name. A String s length is a method You do write the parentheses after the name of the String class s length method.

7.7 Arrays of Objects (1 of 2) Because Strings are objects, we know that arrays can contain objects.

7.7 Arrays of Objects (2 of 2) Each element needs to be initialized. See example: ObjectArray.java

7.10 Two-Dimensional Arrays (1 of 2) A two-dimensional array is an array of arrays. It can be thought of as having rows and columns.

7.10 Two-Dimensional Arrays (2 of 2) Declaring a two-dimensional array requires two sets of brackets and two size declarators The first one is for the number of rows The second one is for the number of columns. The two sets of brackets in the data type indicate that the scores variable will reference a two-dimensional array. Notice that each size declarator is enclosed in its own set of brackets.

Accessing Two-Dimensional Array Elements (1 of 5) When processing the data in a two-dimensional array, each element has two subscripts: one for its row and another for its column.

Accessing Two-Dimensional Array Elements (2 of 5)

Accessing Two-Dimensional Array Elements (3 of 5) Accessing one of the elements in a two-dimensional array requires the use of both subscripts.

Accessing Two-Dimensional Array Elements (4 of 5) Programs that process two-dimensional arrays can do so with nested loops. To fill the scores array:

Accessing Two-Dimensional Array Elements (5 of 5) To print out the scores array: See example: CorpSales.java

Initializing a Two-Dimensional Array (1 of 2) Initializing a two-dimensional array requires enclosing each row s initialization list in its own set of braces. Java automatically creates the array and fills its elements with the initialization values. row 0 {1, 2, 3} row 1 {4, 5, 6} row 2 {7, 8, 9} Declares an array with three rows and three columns.

Initializing a Two-Dimensional Array (2 of 2)

The length Field (1 of 2) Two-dimensional arrays are arrays of one-dimensional arrays. The length field of the array gives the number of rows in the array. Each row has a length constant tells how many columns is in that row. Each row can have a different number of columns.

The length Field (2 of 2) To access the length fields of the array: See example: Lengths.java

Summing the Elements of a Two-Dimensional Array

Summing the Rows of a Two-Dimensional Array

Summing the Columns of a Two-Dimensional Array

More Than Two Dimensions Java does not limit the number of dimensions that an array may be. More than three dimensions is hard to visualize, but can be useful in some programming problems.

Copyright