©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.

Slides:



Advertisements
Similar presentations
Copyright © 2002 Pearson Education, Inc. Slide 1.
Advertisements

Arrays Chapter 6. Outline Array Basics Arrays in Classes and Methods Sorting Arrays Multidimensional Arrays.
©2004 Brooks/Cole Chapter 2 Variables, Values and Operations.
Slides prepared by Rose Williams, Binghamton University Chapter 6 Arrays.
©2004 Brooks/Cole Chapter 6 Methods. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Methods We've already seen examples of using methods.
Arrays  Writing a program that uses a large amount of information.  Such as a list of 100 elements.  It is not practical to declare.
© The McGraw-Hill Companies, 2006 Chapter 5 Arrays.
ECE122 L11: For loops and Arrays March 8, 2007 ECE 122 Engineering Problem Solving with Java Lecture 11 For Loops and Arrays.
Chapter 8. 2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays as Arguments Two-Dimensional Arrays Common.
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.
©TheMcGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter 10 *Arrays with more than one dimension *Java Collections API.
Chapter 9: Arrays and Strings
C++ for Engineers and Scientists Third Edition
Chapter 8 Arrays and Strings
1 CSCE 1030 Computer Science 1 Arrays Chapter 7 in Small Java.
Wednesday, 11/6/02, Slide #1 CS 106 Intro to CS 1 Wednesday, 11/6/02  QUESTIONS?? – HW # 4 due Monday  Today:  Return HW #3  Arrays (Chap. 10)  Reading:
CS 106 Introduction to Computer Science I 02 / 19 / 2007 Instructor: Michael Eckmann.
7.1 Arrays Introduction to arrays Any array is a collection of objects or primitives Useful when the number of reference variables is large or.
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
© 2011 Pearson Education, publishing as Addison-Wesley 1 Arrays  Arrays are objects that help us organize large amounts of information  Chapter 6 focuses.
Chapter 7: Arrays. In this chapter, you will learn about: One-dimensional arrays Array initialization Declaring and processing two-dimensional arrays.
CPS120: Introduction to Computer Science Arrays. Arrays: A Definition A list of variables accessed using a single identifier May be of any data type Can.
A First Book of ANSI C Fourth Edition
Chapter 8 Arrays and Strings
Microsoft Visual C++.NET Chapter 61 Memory Management.
Introduction to Arrays in Java Corresponds with Chapter 6 of textbook.
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2009 Pearson Education, Inc., Upper.
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.
What is an Array? An array is a collection of variables. Arrays have three important properties: –group of related items(for example, temperature for.
© 2007 Lawrenceville Press Slide 1 Chapter 10 Arrays  Can store many of the same kind of data together  Allows a collection of related values to be stored.
Object-Oriented Program Development Using Java: A Class-Centered Approach, Enhanced Edition.
Chapter 8: Collections: Arrays. 2 Objectives One-Dimensional Arrays Array Initialization The Arrays Class: Searching and Sorting Arrays as Arguments The.
Chapter 9 Multidimensional Arrays and the ArrayList Class.
Lecture 5: Arrays A way to organize data MIT AITI April 9th, 2005.
Java Script: Arrays (Chapter 11 in [2]). 2 Outline Introduction Introduction Arrays Arrays Declaring and Allocating Arrays Declaring and Allocating Arrays.
M180: Data Structures & Algorithms in Java Arrays in Java Arab Open University 1.
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.
1 Chapter 7 Arrays. 2 Topics 7.1 Arrays Hold Multiple Values 7.2 Accessing Array Elements 7.3 No Bounds Checking in C Array Initialization 7.5 Processing.
A First Book of C++: From Here To There, Third Edition2 Objectives You should be able to describe: One-Dimensional Arrays Array Initialization Arrays.
Arrays. Related data items Collection of the same types of data. Static entity – Same size throughout program.
CS 139-Programming Fundamentals Lecture 11B - Arrays Adapted from a presentation by Dr. Rahman Fall 2014.
Arrays. Collections We would like to be able to keep lots of information at once Example: Keep all the students in the class Grade each one without writing.
Arrays. The array data structure Array is a collection of elements, that have the same data type Integers (int) Floating point numbers (float, double)
Chapter 8 Arrays. A First Book of ANSI C, Fourth Edition2 Introduction Atomic variable: variable whose value cannot be further subdivided into a built-in.
Lec 13 Oct 21, 02. Array Initialization in the declaration statement ► int temp[5] = {98, 87, 92, 79,85}; ► char codes[6] = { ‘s’, ’a’, ‘m’, ‘p’, ‘l’,
JAVA: An Introduction to Problem Solving & Programming, 5 th Ed. By Walter Savitch and Frank Carrano. ISBN © 2008 Pearson Education, Inc., Upper.
Chapter 8: Part 3 Collections and Two-dimensional arrays.
Arrays Chapter 7. Arrays Hold Multiple Values Array: variable that can store multiple values of the same type Values are stored in adjacent memory locations.
Arrays Chapter 7. MIS Object Oriented Systems Arrays UTD, SOM 2 Objectives Nature and purpose of an array Using arrays in Java programs Methods.
CS 116: Object Oriented Programming II. Topics Vectors Multidimensional Arrays ArrayList.
Arrays Declaring arrays Passing arrays to functions Searching arrays with linear search Sorting arrays with insertion sort Multidimensional arrays Programming.
ADVANCED POINTERS. Overview Review on pointers and arrays Common troubles with pointers Multidimensional arrays Pointers as function arguments Functions.
Chapter 9 Arrays. Chapter Objectives Learn about arrays Explore how to declare and manipulate data into arrays Understand the meaning of “array index.
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.
Objectives You should be able to describe: One-Dimensional Arrays
LESSON 8: INTRODUCTION TO ARRAYS. Lesson 8: Introduction To Arrays Objectives: Write programs that handle collections of similar items. Declare array.
A FIRST BOOK OF C++ CHAPTER 7 ARRAYS. OBJECTIVES In this chapter, you will learn about: One-Dimensional Arrays Array Initialization Arrays as Arguments.
ARRAYS (Extra slides) Arrays are objects that help us organize large amounts of information.
Computer Programming BCT 1113
Two Dimensional Array Mr. Jacobs.
Array, Strings and Vectors
Can store many of the same kind of data together
Introduction To Programming Information Technology , 1’st Semester
Can store many of the same kind of data together
CS2011 Introduction to Programming I Arrays (I)
Peer Instruction 4 Control Loops.
Arrays.
Presentation transcript:

©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 treated similarly Three Lists of Items

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Creating Arrays An array is an indexed collection of data values of the same type. Arrays are reference types (like objects) Three steps to creating an array – Declaration creates a reference variable – Use new to allocate memory – Assign values to individual elements

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Arrays are like Objects Declaring an array allocates memory for a reference –The declaration does NOT allocate memory for the array data The result of the declaration double prices[];

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Allocating Memory for Array The Results of the Allocation price = new double[6]; –All elements are initialized to 0

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Example: A List of Prices Declaration double[] prices; Memory Allocation prices = new double[6] Assigning values prices[0] = 10.96; prices[1] = 6.43; prices[2] = 2.58; prices[3] = 0.58; prices[4] = 12.27; prices[5] = 6.39;

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 More Array Allocations int [] grade = new int[5]; char [] code = new char[4];

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Array Allocation You don't have to use a literal value for the array size –You can use any integer expression Examples int [] grade = new int[NUMELS]; int [] grade = new int[ size * 2];

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Array Size Once memory is allocated, the array has a fixed size Every array has a variable named length associated with it –length contains the number of elements for which memory was allocated –get the length using arrayName.length

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Access to Array Elements Use [ ] to access individual elements of an array –Index can be any integer expression –Individual elements can be used anywhere that a value of the base type can be used –Indexes go from 0 to one less that the length of the array

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Element Access Example grade[3]

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Practice Declare and allocate memory for the following –a list of 100 integer years –a list of 6 character codes –a list of 32 floating point velocities Access the 1st, 3rd and 7th elements of the arrays above

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 More Practice What elements are output by the following for (m=1; m<=5; m++) System.out.println( a[m] + " "); for (int k=1; k< 5; k=k+2) System.out.println( a[k] + " ");

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Array Initializers You can initialize the elements of an array when you declare it int [] grade = {98, 87, 92, 79, 85}; char[] code = {'s', 'a', 'm', 'p', 'l', 'e'}; The size of the array is determined by the number of elements in the initializer list. – grade.length is 5 elements, code.length is 6

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 List of Prices with Initializer Declaration with initializer double[] prices = {10.96, 6.43, 2.58, 0.86, 12.27, 6.39}; The array has 6 elements – prices.length will be 6

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 More Practice Write array declarations with initialization for the following –integer grades 89, 75, 82, 93, 78, 95, 81 –floating point temperatures 78.2, 69.6, 68.5, 83.9, 55.4 –character menu choices p, a, c, w, m, q What is the length of each of the arrays?

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using Arrays Once you have created an array, you can access the elements in any order Use loops to process all elements of the array in the same way –input values for each element –modify each element in the same way –print each element out

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Arrays of Objects Declaration works the same as for arrays of primitive values Memory is allocated to store a reference to the array data String [] names; String names[];

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Allocating Memory for an Array of Strings Instantiation creates an array of references names = new String[4] Each element of the array has to be instantiated individually names[0] = "Joe"; names[1] = new String("Harriet");

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Assignment of Values Creates Actual Array Objects

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Using arrays with methods Arrays can be used as parameters for methods –location of the array is copied into the method parameter variable –the contents of the array can be modified by the method An array can be returned by a method –the location of the array is returned

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Arrays as Method Parameters Only one array exists –method gets a copy of the location

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Arrays as method arguments The Location of the Array is Passed

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Practice Write the header for a method called sort that takes an array of double values as a parameter and returns nothing (modifies the existing array) Write the header for a method called average that takes an array of doubles as a parameter and returns a double Write the header for a method called readArray that takes no parameters and returns an array of integers

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall D Arrays What if we need to make a table instead of a list? –We can make arrays with two indexes Think of 2D array as an array whose elements are arrays

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Creating 2D Arrays Declaration int [][] table Instantiation table = new int[3][4]; Declaration with initialization int[][]table2 = {{8, 16, 9, 52}, {3, 15, 27, 6}, {14, 25, 2, 10}};

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Each Array Element Is Identified by Its Row and Column Position Access row 1 with table[1] Access element 3 of row 2 with table[2][3]

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Practice Write declaration and allocation statements for –an array of integers with 6 rows and 10 columns –an array of doubles with 10 rows and 25 columns Write loops needed to add two 2x3 element arrays on integers and put the results in a new array.

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Representation of a Three-Dimensional Array

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Higher Dimensional arrays are also possible Need one index for each dimension For n dimensions, need n nested loops to process all elements Higher dimensional arrays are harder to visualize

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Deep Copy After the Call to System.arraycopy() newnums = System.arraycopy(nums) –This method creates a new array and copies the values from the original

Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 The Result of a Shallow Copy newnums = nums