Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.

Slides:



Advertisements
Similar presentations
An Array A sequence of elements of a particular type Each element in the array has an index which gives its position in the sequence An array is declared.
Advertisements

CHAPTER 10 ARRAYS II Applications and Extensions.
Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
TK1914: C++ Programming Array II. Objective In this chapter you will explore how to manipulate data in a two-dimensional array. 2FTSM :: TK1914,
Arrays Chapter 6 Chapter 6.
The UNIVERSITY of NORTH CAROLINA at CHAPEL HILL Adrian Ilie COMP 14 Introduction to Programming Adrian Ilie July 14, 2005.
COMP 10 Introduction to Programming Mr. Joshua Stough October 29, 2007.
1 Lecture 21:Arrays and Strings(cont.) Introduction to Computer Science Spring 2006.
©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.
1 Lecture 20:Arrays and Strings Introduction to Computer Science Spring 2006.
1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 8 focuses on: array declaration and use passing arrays and array.
Java Programming: From Problem Analysis to Program Design, 3e Chapter 9 Arrays.
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 9: Arrays and Strings
Chapter 8 Arrays and Strings
Arrays. Objectives Learn about arrays Explore how to declare and manipulate data into arrays Learn about “array index out of bounds” Become familiar with.
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Chapter 9 Introduction to Arrays
Arrays (Part II). Two- and Multidimensional Arrays Two-dimensional array: collection of a fixed number of components (of the same type) arranged in two.
1 Lecture 22:Applications of Arrays Introduction to Computer Science Spring 2006.
COMP 14 Introduction to Programming Miguel A. Otaduy June 1, 2004.
Prepared by MURLI MANOHAR PGT (COMPUTER SCIENCE) KV,B.E.G., PUNE.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Arrays in C++ Numeric Character. Structured Data Type A structured data type is a type that stores a collection of individual components with one variable.
Chapter 8 Arrays and Strings
French Territory of St. Pierre CSE 114 – Computer Science I Arrays.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
EGR 2261 Unit 8 One-dimensional Arrays  Read Malik, pages in Chapter 8.  Homework #8 and Lab #8 due next week.  Quiz next week.
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.
Java Programming: From Problem Analysis to Program Design, 4e
Java Basics Elizabeth MacDonald
ARRAYS 1 TOPIC 8 l Array Basics l Arrays and Methods l Programming with Arrays Arrays.
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition Second Edition.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Arrays.
© 2004 Pearson Addison-Wesley. All rights reserved October 13, D Arrays ComS 207: Programming I (in Java) Iowa State University, FALL 2006 Instructor:
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
Arrays  Array is a collection of same type elements under the same variable identifier referenced by index number.  Arrays are widely used within programming.
CHAPTER 7 arrays I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
1 Topic: Array Topic: Array. 2 Arrays Arrays In this chapter, we will : Learn about arrays Learn about arrays Explore how to declare and manipulate data.
1 Arrays and Strings Lecture: Design Problem l Consider a program to calculate class average Why?? ?
UniMAP Sem2-10/11 DKT121: Fundamental of Computer Programming1 Arrays.
Arrays.
Arrays An array is an indexed data structure which is used to store data elements of the same data type. An array is an indexed data structure which is.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
Chapter 5: ARRAYS ARRAYS. Why Do We Need Arrays? Java Programming: From Problem Analysis to Program Design, 4e 2  We want to write a Java program that.
CiS 260: App Dev I. 2 Introduction to Arrays n An array is an object that contains a collection of components (_________) of the same data type. n For.
Java Programming: Chapter 9: Arrays
Opening Input/Output Files ifstream infile; ofstream outfile; char inFileName[40]; char outFileName[40]; coutinFileName;
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.
1 Chapter 9 Arrays Java Programming from Thomson Course Tech, adopted by kcluk.
SEQUENTIAL AND OBJECT ORIENTED PROGRAMMING Arrays.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
Arrays. Arrays are objects that help us organize large amounts of information.
VISUAL C++ PROGRAMMING: CONCEPTS AND PROJECTS Chapter 7A Arrays (Concepts)
Chapter 9 Introduction to Arrays Fundamentals of Java.
1 Why do we need arrays? Problem - Input 5 test scores => int test1,test2,test3,test4,test5 100 test scores? 10,000 employees? A structured data type is.
KUKUM-06/07 EKT120: Computer Programming 1 Week 6 Arrays-Part 1.
Lesson 9 Arrays. Miscellaneous About Arrays An array is an object. Because of this, the array name is a reference variable Therefore, in order to start.
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
Array 1 ARRAY. array 2 Learn about arrays. Explore how to declare and manipulate data into arrays. Understand the meaning of “array index out of bounds.”
1 Arrays and Variable Length Parameter List  The syntax to declare a variable length formal parameter (list) is: dataType... identifier.
Chapter 9: Arrays J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design,
EGR 2261 Unit 10 Two-dimensional Arrays
Chapter 8: Arrays Starting Out with C++ Early Objects Ninth Edition
Computer Programming BCT 1113
Lecture 9 Objectives Learn about arrays.
Java Programming: Program Design Including Data Structures
Presentation transcript:

Chapter 9 Arrays

Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index out of bounds” Become familiar with the restrictions on array processing

Chapter Objectives Discover how to pass an array as a parameter to a method Discover how to manipulate data in a two- dimensional array Learn about multidimensional arrays

Array Definition: structured (or aggregate) data type with a fixed number of components Every component is of the same type Components are accessed using their relative positions in the array

One-Dimensional Arrays [ ] is the array subscripting operator Syntax to instantiate an array: –dataType[ ] arrayName; arrayName = new dataType[intExp] –dataType[ ] arrayName = new dataType[intExp] –dataType[ ] arrayName1, arrayName2; –dataType arrayName1[ ], variableName1; Syntax to access an array component: –arrayName[indexExp] intExp = number of components in array >= 0 0 <= indexExp < intExp

Initializing Arrays int[] list = {10, 20, 30, 40, 50} Creates the integer array list with length 5 and initializes the components to the given values

Array num: int[] num = new int[5];

Array list

Arrays Not necessary to know array size at compile time arrayName.length returns the number of components in array Loops used to step through elements in array and perform operations

Arrays Some operations on arrays: –Initialize –Input data –Output stored data –Find largest/smallest/sum/average of elements

How To Specify Array Size During Program Execution int arraySize; //Line 1 System.out.print("Enter the size of the array: "); //Line 2 arraySize = Integer.parseInt(keyboard.readLine()); //Line 3 System.out.println(); //Line 4 int[] list = new int[arraySize]; //Line 5

Instance Variable length Contains size of array public member Can be directly accessed in program using array name and dot operator Example –If: int[] list = {10, 20, 30, 40, 50, 60}; –Then: list.length is 6

Code to Initialize Array to Specific Value (10.00) for(index = 0; index < sale.length; index++) sale[index] = 10.00;

Code to Read Data into Array for(index = 0; index < sale.length; index++) sale[index] = Integer.parseInt(keyboard.readLine());

Code to Print Array for(index = 0; index < sale.length; index++) System.out.print(sale[index] + " ");

Code to Find Sum and Average of Array sum = 0; for(index = 0; index < sale.length; index++) sum = sum + sale[index]; if(sale.length != 0) average = sum / sale.length; else average = 0.0;

Determining Largest Element in Array maxIndex = 0; for(index = 1; index < sale.length; index++) if(sale[maxIndex] < sale[index]) maxIndex = index; largestSale = sale[maxIndex];

Determining Largest Element in Array

Array Index Out of Bounds Array in bounds if: 0 <= index <= arraySize – 1 If index = arraySize: ArrayIndexOutOfBoundsException exception is thrown Base address: memory location of first component in array

The Assignment Operator, the Relational Operator, and Arrays

Methods for Array Processing

Parallel Arrays Arrays are parallel if corresponding components hold related information

Arrays of Objects Can use arrays to manipulate objects Example: create array named array1 with N objects of type T T [ ] array1 = new T[N] Can instantiate elements of array1 as follows: for(int j=0; j <array1.length; j++) array1[j] = new T();

Arrays of Objects: Clock[] arrivalTimeEmp = new Clock [100];

Instantiating Array Objects

Two-Dimensional Arrays Data is sometimes in table form (difficult to represent using one-dimensional array). Think in terms of an 1-D array of 1-D arrays. To declare/instantiate two-dimensional array: dataType[ ][ ] arrayName = new dataType[intExp1][intExp2]; To access a component of a 2-dimensional array: arrayName[indexExp1][indexExp2]; intExp1, intExp2 >= 0 indexExp1 = row position indexExp2 = column position

Two-Dimensional Arrays Can specify different number of columns for each row (ragged arrays) int twoD[ ][ ] = new int[3][ ]; twoD[0] = new int[5]; twoD[1] = new int[3]; twoD[2] = new int[7]; twoD.length gives the number of rows twoD[i].length gives the number of elements in row I (number of columns)

Two-Dimensional Arrays Initialization at Declaration int[ ] [ ] mine = { { 2, 3 }, {15, 25, 13}, {20, 4, 7, 8} }; Three ways to process 2-D arrays –Entire array –Particular row of array (row processing) –Particular column of array (column processing) Processing algorithms similar to processing algorithms of one-dimensional arrays

Two-Dimensional Arrays: Special Cases

double[][]sales = new double[10][5]; Two-Dimensional Arrays

Accessing Two-Dimensional Array Components

Multidimensional Arrays Can define three-dimensional arrays or n- dimensional array (n can be any number) Syntax to declare and instantiate array: d ataType[ ][ ]…[ ] arrayName = new dataType[intExp1][intExp2]…[intExpn]; Syntax to access component: arrayName[indexExp1][indexExp2]…[indexExpn] intExp1, intExp2,..., intExpn = positive integers indexExp1,indexExp2,..., indexExpn = non-negative integers

Loops to Process Multidimensional Arrays double[][][] carDealers = new double [10][5][7]; for(i = 0; i < 10; i++) for(j = 0; j < 5; j++) for(k = 0; k < 7; k++) carDealers[i][j][k] = 10.00;