Arrays Chapter 7. 2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must.

Slides:



Advertisements
Similar presentations
Arrays.
Advertisements

Introduction to Programming Lecture 15. In Today’s Lecture Pointers and Arrays Manipulations Pointers and Arrays Manipulations Pointers Expression Pointers.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
For use of IST410 Students only Arrays-1 Arrays. For use of IST410 Students only Arrays-2 Objectives l Declaring arrays l Instantiating arrays l Using.
 2003 Prentice Hall, Inc. All rights reserved. 7.1 Introduction Arrays –Data structures which reference one or more value –All items must have same data.
Java: How to Program Arrays and ArrayLists Summary Yingcai Xiao.
©2004 Brooks/Cole Chapter 8 Arrays. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Sometimes we have lists of data values that all need to be.
 2003 Prentice Hall, Inc. All rights reserved. Chapter 7 - Arrays Outline 7.1 Introduction 7.2 Arrays 7.3 Declaring and Creating Arrays 7.4 Examples Using.
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2003 Prentice Hall, Inc. All rights reserved. Modified for use with this course. Introduction to Arrays Introduction to Computers and Programming in.
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
 Pearson Education, Inc. All rights reserved Arrays.
1 JavaScript/Jscript: Arrays. 2 Introduction Arrays –Data structures consisting of related data items (collections of data items) JavaScript arrays are.
1 JavaScript: Functions and Arrays October 18, 2005 Slides modified from Internet & World Wide Web: How to Program (3rd) edition. By Deitel, Deitel,
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
 2004 Prentice Hall, Inc. All rights reserved. 1 Chapter 11 - JavaScript: Arrays Outline 11.1 Introduction 11.2 Arrays 11.3 Declaring and Allocating Arrays.
The University of Texas – Pan American
Chapter 8 Arrays and Strings
Session 7 JavaScript/Jscript: Arrays Matakuliah: M0114/Web Based Programming Tahun: 2005 Versi: 5.
Arrays and Strings Introducing Arrays Declaring Arrays Creating Arrays Initializing Arrays Array of Objects Copying Arrays Multidimensional Arrays Command-Line.
Arrays Chapter 7. 2 "All students to receive arrays!" reports Dr. Austin. Declaring arrays scores : Inspecting.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
ARRAYS 1 Week 2. Data Structures  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently 
 Pearson Education, Inc. All rights reserved Arrays.
 2005 Pearson Education, Inc. All rights reserved. 1 Arrays.
 Pearson Education, Inc. All rights reserved Arrays.
Arrays Module 6. Objectives Nature and purpose of an array Using arrays in Java programs Methods with array parameter Methods that return an array Array.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 7 Arrays. A 12-element array Declaring and Creating Arrays Arrays are objects that occupy memory Created dynamically with keyword new int c[]
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
Arrays Array – Group of contiguous memory locations Each memory location has same name Each memory location has same type.
Java™ How to Program, 9/e © Copyright by Pearson Education, Inc. All Rights Reserved.
C++ for Engineers and Scientists Second Edition Chapter 11 Arrays.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
 2005 Pearson Education, Inc. All rights reserved Arrays.
1 Building Java Programs Chapter 7: Arrays These lecture notes are copyright (C) Marty Stepp and Stuart Reges, They may not be rehosted, sold, or.
Week # 2: Arrays.  Data structure  A particular way of storing and organising data in a computer so that it can be used efficiently  Types of data.
 Pearson Education, Inc. All rights reserved Arrays.
 2007 Pearson Education, Inc. All rights reserved C Arrays.
Chapter 9 Processing Lists with Arrays. Class 9: Arrays Understand the concept of random numbers and how to generate random numbers Describe the similarities.
 2008 Pearson Education, Inc. All rights reserved. 1 Arrays and Vectors.
How do you do the following? Find the number of scores within 3 points of the average of 10 scores? What kind of a tool do you need? Today’s notes: Include.
Computer Science: A Structured Programming Approach Using C1 8-7 Two-Dimensional Arrays The arrays we have discussed so far are known as one- dimensional.
Arrays Chapter 6. Objectives learn about arrays and how to use them in Java programs learn how to use array parameters and how to define methods that.
 Pearson Education, Inc. All rights reserved Passing Arrays to Methods To pass array argument to a method – Specify array name without.
Arrays (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
C++ Array 1. C++ provides a data structure, the array, which stores a fixed-size sequential collection of elements of the same type. An array is used.
ARRAYS Multidimensional realities Image courtesy of
 2005 Pearson Education, Inc. All rights reserved Arrays.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Arrays.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Introduction to programming in java Lecture 21 Arrays – Part 1.
A DVANCED P ROGRAMMING C HAPTER 8: A RRAYS Dr Shahriar Bijani Spring 2016.
Lecture 4: Chapter 7 - Arrays Outline Declaring and Creating Arrays Examples Using Arrays References and Reference Parameters Passing Arrays to Methods.
Chapter 11 - JavaScript: Arrays
Arrays Chapter 7.
The University of Texas – Pan American
Java How to Program, Late Objects Version, 10/e
7 Arrays.
Introduction To Programming Information Technology , 1’st Semester
Object Oriented Programming in java
CS2011 Introduction to Programming I Arrays (I)
MSIS 655 Advanced Business Applications Programming
Arrays Chapter 7.
CSCI 3328 Object Oriented Programming in C# Chapter 7: Arrays – Exercises UTPA – Fall 2012 This set of slides is revised from lecture slides of Prof.
Presentation transcript:

Arrays Chapter 7

2 Declaring and Creating Arrays Recall that an array is a collection of elements all of the _____________ Array objects in Java must be created with the key word _______________ Syntax int c[] = new int [12]; Results: –12 integer locations are allocated –They are _____________________ (null for reference variables, false for boolean)

3 Using Arrays View example Figure 7.2Figure 7.2 Note –Declaration of the array –Allocation of memory with new –Initialization of array elements –Use of array attribute ( _______________ ) –Printing of array contents

4 The Array Initializer A comma separated list of expressions –The ______________ list –Enclosed in _________________ View the ExampleExample –Note again, how the array knows its own _______ for (int count = 0 ; count < array.length ; count++)...

5 Processing Elements of An Array Finding and using the arithmetic mean –__________________ the array, create –Read in values, ___________________ –Declare a summation variable, initialize –______________ the array, calculate average –_______________ the array –Print results, accessing the array See sample programsample program

6 Histogram Program Use the value in the array to –Print the value –Print a string of asterisks for __________________ of size of the value View Figure 7.6,Figure 7.6

7 Using Array Elements as Counters Recall program which tested random number generator (Figure 6.8)Figure 6.8 –Used 6 different variables –Used switch statement to increment –At the time we noted the ________________ Consider a new version of the program –Uses _______________ to total the number of rolls of each number 1 – 6 –No ___________ statement needed View Figure 7.7Figure 7.7

8 Enhanced for Statement Enhanced for statement –New feature of J2SE 5.0 –Allows iterates through elements of an array or a collection _______________________ –Syntax for ( parameter : arrayName ) statement –View example, Figure 7.12Figure 7.12

9 References and Reference Parameters In Java, primitive-type variables are always passed by _________ –Incoming data only Objects are not passed to methods –__________ to objects are passed –The reference is passed by value With a reference to the object the method can _____________ the object directly

10 References and Reference Parameters Passing arrays by reference –Improves _______________ –Saves time ________________ of large array not required –Saves _______________ Use memory already occupied by array No new memory allocation required –View example program, Figure 7.13Figure 7.13

11 Using Arrays Sorting Arrays –Recall previous exampleprevious example Searching Arrays –________________ search (see example)example Step through array until desired value located –Binary search Array must be _________________ Look in middle, then above or below –Look in middle of sub-section then above or below –Etc.

12 Multidimensional Arrays Java does not support ________________ arrays directly Does allow declaration of arrays whose elements are ______________! int b[][]; b = new int[ 2 ][ ]; // allocate rows b[ 0 ] = new int[ 5 ]; // allocate columns for row 0 b[ 1 ] = new int[ 3 ]; // allocate columns for row 1

13 Fig | Two-dimensional array with three rows and four columns. Multidimensional Arrays

14 Creating Two-dimensional Arrays –Can be created dynamically –3 -by- 4 array int b[][]; b = new int[ 3 ][ 4 ]; –Rows can have different number of ____________ int b[][]; b = new int[ 2 ][ ]; // create 2 rows b[ 0 ] = new int[ 5 ]; // create 5 columns for row 0 b[ 1 ] = new int[ 3 ]; // create 3 columns for row

15 Variable-Length Argument Lists New feature in J2SE 5.0 ___________________ number of arguments Use ellipsis ( … ) in method’s parameter list –Can occur _______________ in parameter list –Must be placed at the __________ of parameter list Array whose elements are all of the same type View example, Fig. 7.20Fig. 7.20

16 Using Command-Line Arguments Pass arguments from the command line –String args[] Appear after the _____________ in the java command –java MyClass a b _________________ of arguments passed in from command line –args.length First command-line argument –args[ 0 ]